@jambonz/sdk 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/dist/client/index.cjs +119 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +58 -0
- package/dist/client/index.d.ts +58 -0
- package/dist/client/index.js +115 -0
- package/dist/client/index.js.map +1 -0
- package/dist/common-Bsqv1Brz.d.cts +69 -0
- package/dist/common-Bsqv1Brz.d.ts +69 -0
- package/dist/index.cjs +1014 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +996 -0
- package/dist/index.js.map +1 -0
- package/dist/rest-BV_FsqnH.d.cts +97 -0
- package/dist/rest-CthGXHnw.d.ts +97 -0
- package/dist/types/index.cjs +4 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +3 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/verb-builder-But-DJle.d.cts +82 -0
- package/dist/verb-builder-CO_qoYxC.d.ts +82 -0
- package/dist/verbs-B_afNNUM.d.cts +770 -0
- package/dist/verbs-B_afNNUM.d.ts +770 -0
- package/dist/webhook/index.cjs +320 -0
- package/dist/webhook/index.cjs.map +1 -0
- package/dist/webhook/index.d.cts +26 -0
- package/dist/webhook/index.d.ts +26 -0
- package/dist/webhook/index.js +313 -0
- package/dist/webhook/index.js.map +1 -0
- package/dist/websocket/index.cjs +833 -0
- package/dist/websocket/index.cjs.map +1 -0
- package/dist/websocket/index.d.cts +179 -0
- package/dist/websocket/index.d.ts +179 -0
- package/dist/websocket/index.js +822 -0
- package/dist/websocket/index.js.map +1 -0
- package/package.json +96 -0
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import { WebSocketServer } from 'ws';
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import Ajv from 'ajv';
|
|
5
|
+
import { readFileSync } from 'fs';
|
|
6
|
+
import { dirname, resolve } from 'path';
|
|
7
|
+
import { fileURLToPath } from 'url';
|
|
8
|
+
|
|
9
|
+
// src/websocket/endpoint.ts
|
|
10
|
+
var COMPONENT_SCHEMAS = [
|
|
11
|
+
"auth",
|
|
12
|
+
"actionHook",
|
|
13
|
+
"synthesizer",
|
|
14
|
+
"recognizer",
|
|
15
|
+
"target",
|
|
16
|
+
"vad",
|
|
17
|
+
"bidirectionalAudio",
|
|
18
|
+
"fillerNoise",
|
|
19
|
+
"actionHookDelayAction"
|
|
20
|
+
];
|
|
21
|
+
var VERB_SCHEMAS = [
|
|
22
|
+
"answer",
|
|
23
|
+
"alert",
|
|
24
|
+
"config",
|
|
25
|
+
"say",
|
|
26
|
+
"play",
|
|
27
|
+
"gather",
|
|
28
|
+
"dial",
|
|
29
|
+
"listen",
|
|
30
|
+
"stream",
|
|
31
|
+
"llm",
|
|
32
|
+
"pipeline",
|
|
33
|
+
"conference",
|
|
34
|
+
"transcribe",
|
|
35
|
+
"enqueue",
|
|
36
|
+
"dequeue",
|
|
37
|
+
"dtmf",
|
|
38
|
+
"dub",
|
|
39
|
+
"hangup",
|
|
40
|
+
"leave",
|
|
41
|
+
"message",
|
|
42
|
+
"pause",
|
|
43
|
+
"redirect",
|
|
44
|
+
"tag",
|
|
45
|
+
"sip-decline",
|
|
46
|
+
"sip-request",
|
|
47
|
+
"sip-refer"
|
|
48
|
+
];
|
|
49
|
+
function loadSchema(schemaDir, relativePath) {
|
|
50
|
+
const fullPath = resolve(schemaDir, relativePath);
|
|
51
|
+
return JSON.parse(readFileSync(fullPath, "utf-8"));
|
|
52
|
+
}
|
|
53
|
+
function findSchemaDir() {
|
|
54
|
+
const currentDir = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
|
|
55
|
+
const candidates = [
|
|
56
|
+
resolve(currentDir, "../../schema"),
|
|
57
|
+
// from src/
|
|
58
|
+
resolve(currentDir, "../../../schema")
|
|
59
|
+
// from dist/
|
|
60
|
+
];
|
|
61
|
+
for (const candidate of candidates) {
|
|
62
|
+
try {
|
|
63
|
+
readFileSync(resolve(candidate, "jambonz-app.schema.json"), "utf-8");
|
|
64
|
+
return candidate;
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
throw new Error(
|
|
69
|
+
"Could not find jambonz schema directory. Ensure the schema/ directory is present alongside the typescript/ directory."
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
var JambonzValidator = class {
|
|
73
|
+
validateApp;
|
|
74
|
+
ajv;
|
|
75
|
+
constructor(schemaDir) {
|
|
76
|
+
const dir = schemaDir ?? findSchemaDir();
|
|
77
|
+
this.ajv = new Ajv({
|
|
78
|
+
allErrors: true,
|
|
79
|
+
strict: false,
|
|
80
|
+
validateSchema: false,
|
|
81
|
+
logger: false
|
|
82
|
+
});
|
|
83
|
+
for (const name of COMPONENT_SCHEMAS) {
|
|
84
|
+
const schema = loadSchema(dir, `components/${name}.schema.json`);
|
|
85
|
+
this.ajv.addSchema(schema);
|
|
86
|
+
}
|
|
87
|
+
for (const name of VERB_SCHEMAS) {
|
|
88
|
+
const schema = loadSchema(dir, `verbs/${name}.schema.json`);
|
|
89
|
+
this.ajv.addSchema(schema);
|
|
90
|
+
}
|
|
91
|
+
const appSchema = loadSchema(dir, "jambonz-app.schema.json");
|
|
92
|
+
this.validateApp = this.ajv.compile(appSchema);
|
|
93
|
+
}
|
|
94
|
+
/** Validate a complete verb array (a jambonz application). */
|
|
95
|
+
validate(app) {
|
|
96
|
+
const valid = this.validateApp(app);
|
|
97
|
+
if (valid) {
|
|
98
|
+
return { valid: true, errors: [] };
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
valid: false,
|
|
102
|
+
errors: formatErrors(this.validateApp.errors)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** Validate a single verb object. */
|
|
106
|
+
validateVerb(verb) {
|
|
107
|
+
return this.validate([verb]);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
function formatErrors(errors) {
|
|
111
|
+
if (!errors) return [];
|
|
112
|
+
return errors.map((err) => ({
|
|
113
|
+
path: err.instancePath || "/",
|
|
114
|
+
message: err.message || "Unknown validation error"
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/verb-builder.ts
|
|
119
|
+
var VerbBuilder = class {
|
|
120
|
+
verbs = [];
|
|
121
|
+
validator;
|
|
122
|
+
constructor(opts) {
|
|
123
|
+
if (opts?.validate === false) {
|
|
124
|
+
this.validator = null;
|
|
125
|
+
} else {
|
|
126
|
+
this.validator = new JambonzValidator(opts?.schemaDir);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// --- Audio & Speech ---
|
|
130
|
+
say(opts) {
|
|
131
|
+
return this.addVerb({ verb: "say", ...opts });
|
|
132
|
+
}
|
|
133
|
+
play(opts) {
|
|
134
|
+
return this.addVerb({ verb: "play", ...opts });
|
|
135
|
+
}
|
|
136
|
+
gather(opts) {
|
|
137
|
+
return this.addVerb({ verb: "gather", ...opts });
|
|
138
|
+
}
|
|
139
|
+
// --- AI & Real-time ---
|
|
140
|
+
llm(opts) {
|
|
141
|
+
return this.addVerb({ verb: "llm", ...opts });
|
|
142
|
+
}
|
|
143
|
+
pipeline(opts) {
|
|
144
|
+
return this.addVerb({ verb: "pipeline", ...opts });
|
|
145
|
+
}
|
|
146
|
+
listen(opts) {
|
|
147
|
+
return this.addVerb({ verb: "listen", ...opts });
|
|
148
|
+
}
|
|
149
|
+
stream(opts) {
|
|
150
|
+
return this.addVerb({ verb: "stream", ...opts });
|
|
151
|
+
}
|
|
152
|
+
transcribe(opts) {
|
|
153
|
+
return this.addVerb({ verb: "transcribe", ...opts });
|
|
154
|
+
}
|
|
155
|
+
// --- Call Control ---
|
|
156
|
+
dial(opts) {
|
|
157
|
+
return this.addVerb({ verb: "dial", ...opts });
|
|
158
|
+
}
|
|
159
|
+
conference(opts) {
|
|
160
|
+
return this.addVerb({ verb: "conference", ...opts });
|
|
161
|
+
}
|
|
162
|
+
enqueue(opts) {
|
|
163
|
+
return this.addVerb({ verb: "enqueue", ...opts });
|
|
164
|
+
}
|
|
165
|
+
dequeue(opts) {
|
|
166
|
+
return this.addVerb({ verb: "dequeue", ...opts });
|
|
167
|
+
}
|
|
168
|
+
hangup(opts) {
|
|
169
|
+
return this.addVerb({ verb: "hangup", ...opts });
|
|
170
|
+
}
|
|
171
|
+
redirect(opts) {
|
|
172
|
+
return this.addVerb({ verb: "redirect", ...opts });
|
|
173
|
+
}
|
|
174
|
+
pause(opts) {
|
|
175
|
+
return this.addVerb({ verb: "pause", ...opts });
|
|
176
|
+
}
|
|
177
|
+
leave(opts) {
|
|
178
|
+
return this.addVerb({ verb: "leave", ...opts });
|
|
179
|
+
}
|
|
180
|
+
// --- SIP ---
|
|
181
|
+
sipDecline(opts) {
|
|
182
|
+
return this.addVerb({ verb: "sip:decline", ...opts });
|
|
183
|
+
}
|
|
184
|
+
sipRequest(opts) {
|
|
185
|
+
return this.addVerb({ verb: "sip:request", ...opts });
|
|
186
|
+
}
|
|
187
|
+
sipRefer(opts) {
|
|
188
|
+
return this.addVerb({ verb: "sip:refer", ...opts });
|
|
189
|
+
}
|
|
190
|
+
// --- Utility ---
|
|
191
|
+
config(opts) {
|
|
192
|
+
return this.addVerb({ verb: "config", ...opts });
|
|
193
|
+
}
|
|
194
|
+
answer(opts) {
|
|
195
|
+
return this.addVerb({ verb: "answer", ...opts });
|
|
196
|
+
}
|
|
197
|
+
alert(opts) {
|
|
198
|
+
return this.addVerb({ verb: "alert", ...opts });
|
|
199
|
+
}
|
|
200
|
+
tag(opts) {
|
|
201
|
+
return this.addVerb({ verb: "tag", ...opts });
|
|
202
|
+
}
|
|
203
|
+
dtmf(opts) {
|
|
204
|
+
return this.addVerb({ verb: "dtmf", ...opts });
|
|
205
|
+
}
|
|
206
|
+
dub(opts) {
|
|
207
|
+
return this.addVerb({ verb: "dub", ...opts });
|
|
208
|
+
}
|
|
209
|
+
message(opts) {
|
|
210
|
+
return this.addVerb({ verb: "message", ...opts });
|
|
211
|
+
}
|
|
212
|
+
// --- Raw JSON input (primary AI agent path) ---
|
|
213
|
+
/**
|
|
214
|
+
* Add one or more verbs from raw JSON.
|
|
215
|
+
* Validates against the JSON schemas at runtime if validation is enabled.
|
|
216
|
+
*/
|
|
217
|
+
addVerbs(verbs) {
|
|
218
|
+
if (this.validator) {
|
|
219
|
+
const result = this.validator.validate(verbs);
|
|
220
|
+
if (!result.valid) {
|
|
221
|
+
const messages = result.errors.map((e) => `${e.path}: ${e.message}`).join("; ");
|
|
222
|
+
throw new Error(`Verb validation failed: ${messages}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
this.verbs.push(...verbs);
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
// --- Internal ---
|
|
229
|
+
addVerb(verb) {
|
|
230
|
+
if (this.validator) {
|
|
231
|
+
const result = this.validator.validateVerb(verb);
|
|
232
|
+
if (!result.valid) {
|
|
233
|
+
const messages = result.errors.map((e) => `${e.path}: ${e.message}`).join("; ");
|
|
234
|
+
throw new Error(`Verb '${verb.verb}' validation failed: ${messages}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
this.verbs.push(verb);
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
/** Get the current verb count. */
|
|
241
|
+
get length() {
|
|
242
|
+
return this.verbs.length;
|
|
243
|
+
}
|
|
244
|
+
/** Clear all queued verbs. */
|
|
245
|
+
clear() {
|
|
246
|
+
this.verbs = [];
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
/** Returns a copy of the current verb array. */
|
|
250
|
+
toJSON() {
|
|
251
|
+
return [...this.verbs];
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// src/websocket/session.ts
|
|
256
|
+
var Session = class extends EventEmitter {
|
|
257
|
+
callSid;
|
|
258
|
+
b3;
|
|
259
|
+
data;
|
|
260
|
+
/** Application-specific storage that persists for the session. */
|
|
261
|
+
locals = {};
|
|
262
|
+
ws;
|
|
263
|
+
logger;
|
|
264
|
+
msgid;
|
|
265
|
+
acked = false;
|
|
266
|
+
builder;
|
|
267
|
+
commandQueue = [];
|
|
268
|
+
ttsCounter = 0;
|
|
269
|
+
ttsPaused = false;
|
|
270
|
+
constructor(opts) {
|
|
271
|
+
super();
|
|
272
|
+
this.ws = opts.ws;
|
|
273
|
+
this.logger = opts.logger;
|
|
274
|
+
this.msgid = opts.msg.msgid;
|
|
275
|
+
this.callSid = opts.msg.data?.call_sid ?? opts.msg.call_sid ?? "";
|
|
276
|
+
this.b3 = opts.msg.b3;
|
|
277
|
+
this.data = opts.msg.data ?? {};
|
|
278
|
+
this.builder = new VerbBuilder({
|
|
279
|
+
validate: opts.validate,
|
|
280
|
+
schemaDir: opts.schemaDir
|
|
281
|
+
});
|
|
282
|
+
this.ws.on("message", (data) => {
|
|
283
|
+
this.onMessage(data);
|
|
284
|
+
});
|
|
285
|
+
this.ws.on("close", (code, reason) => {
|
|
286
|
+
this.emit("close", code, reason);
|
|
287
|
+
});
|
|
288
|
+
this.ws.on("error", (err) => {
|
|
289
|
+
this.emit("error", err);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
// ---------------------------------------------------------------------------
|
|
293
|
+
// Verb builder delegation — same API as VerbBuilder
|
|
294
|
+
// ---------------------------------------------------------------------------
|
|
295
|
+
say(opts) {
|
|
296
|
+
this.builder.say(opts);
|
|
297
|
+
return this;
|
|
298
|
+
}
|
|
299
|
+
play(opts) {
|
|
300
|
+
this.builder.play(opts);
|
|
301
|
+
return this;
|
|
302
|
+
}
|
|
303
|
+
gather(opts) {
|
|
304
|
+
this.builder.gather(opts);
|
|
305
|
+
return this;
|
|
306
|
+
}
|
|
307
|
+
llm(opts) {
|
|
308
|
+
this.builder.llm(opts);
|
|
309
|
+
return this;
|
|
310
|
+
}
|
|
311
|
+
pipeline(opts) {
|
|
312
|
+
this.builder.pipeline(opts);
|
|
313
|
+
return this;
|
|
314
|
+
}
|
|
315
|
+
listen(opts) {
|
|
316
|
+
this.builder.listen(opts);
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
stream(opts) {
|
|
320
|
+
this.builder.stream(opts);
|
|
321
|
+
return this;
|
|
322
|
+
}
|
|
323
|
+
transcribe(opts) {
|
|
324
|
+
this.builder.transcribe(opts);
|
|
325
|
+
return this;
|
|
326
|
+
}
|
|
327
|
+
dial(opts) {
|
|
328
|
+
this.builder.dial(opts);
|
|
329
|
+
return this;
|
|
330
|
+
}
|
|
331
|
+
conference(opts) {
|
|
332
|
+
this.builder.conference(opts);
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
enqueue(opts) {
|
|
336
|
+
this.builder.enqueue(opts);
|
|
337
|
+
return this;
|
|
338
|
+
}
|
|
339
|
+
dequeue(opts) {
|
|
340
|
+
this.builder.dequeue(opts);
|
|
341
|
+
return this;
|
|
342
|
+
}
|
|
343
|
+
hangup(opts) {
|
|
344
|
+
this.builder.hangup(opts);
|
|
345
|
+
return this;
|
|
346
|
+
}
|
|
347
|
+
redirect(opts) {
|
|
348
|
+
this.builder.redirect(opts);
|
|
349
|
+
return this;
|
|
350
|
+
}
|
|
351
|
+
pause(opts) {
|
|
352
|
+
this.builder.pause(opts);
|
|
353
|
+
return this;
|
|
354
|
+
}
|
|
355
|
+
leave(opts) {
|
|
356
|
+
this.builder.leave(opts);
|
|
357
|
+
return this;
|
|
358
|
+
}
|
|
359
|
+
sipDecline(opts) {
|
|
360
|
+
this.builder.sipDecline(opts);
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
363
|
+
sipRequest(opts) {
|
|
364
|
+
this.builder.sipRequest(opts);
|
|
365
|
+
return this;
|
|
366
|
+
}
|
|
367
|
+
sipRefer(opts) {
|
|
368
|
+
this.builder.sipRefer(opts);
|
|
369
|
+
return this;
|
|
370
|
+
}
|
|
371
|
+
config(opts) {
|
|
372
|
+
this.builder.config(opts);
|
|
373
|
+
return this;
|
|
374
|
+
}
|
|
375
|
+
answer(opts) {
|
|
376
|
+
this.builder.answer(opts);
|
|
377
|
+
return this;
|
|
378
|
+
}
|
|
379
|
+
alert(opts) {
|
|
380
|
+
this.builder.alert(opts);
|
|
381
|
+
return this;
|
|
382
|
+
}
|
|
383
|
+
tag(opts) {
|
|
384
|
+
this.builder.tag(opts);
|
|
385
|
+
return this;
|
|
386
|
+
}
|
|
387
|
+
dtmf(opts) {
|
|
388
|
+
this.builder.dtmf(opts);
|
|
389
|
+
return this;
|
|
390
|
+
}
|
|
391
|
+
dub(opts) {
|
|
392
|
+
this.builder.dub(opts);
|
|
393
|
+
return this;
|
|
394
|
+
}
|
|
395
|
+
message(opts) {
|
|
396
|
+
this.builder.message(opts);
|
|
397
|
+
return this;
|
|
398
|
+
}
|
|
399
|
+
/** Add raw JSON verbs (primary AI agent path). */
|
|
400
|
+
addVerbs(verbs) {
|
|
401
|
+
this.builder.addVerbs(verbs);
|
|
402
|
+
return this;
|
|
403
|
+
}
|
|
404
|
+
// ---------------------------------------------------------------------------
|
|
405
|
+
// Send / Reply
|
|
406
|
+
// ---------------------------------------------------------------------------
|
|
407
|
+
/** Send queued verbs to jambonz. */
|
|
408
|
+
send(opts) {
|
|
409
|
+
const execImmediate = opts?.execImmediate ?? true;
|
|
410
|
+
const data = this.builder.toJSON();
|
|
411
|
+
this.builder.clear();
|
|
412
|
+
if (!this.acked) {
|
|
413
|
+
this.acked = true;
|
|
414
|
+
const response = {
|
|
415
|
+
type: "ack",
|
|
416
|
+
msgid: this.msgid,
|
|
417
|
+
data
|
|
418
|
+
};
|
|
419
|
+
this.wsSend(response);
|
|
420
|
+
} else {
|
|
421
|
+
const response = {
|
|
422
|
+
type: "command",
|
|
423
|
+
command: "redirect",
|
|
424
|
+
queueCommand: !execImmediate,
|
|
425
|
+
data
|
|
426
|
+
};
|
|
427
|
+
this.wsSend(response);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/** Reply to an actionHook/eventHook with queued verbs. */
|
|
431
|
+
reply(opts) {
|
|
432
|
+
const execImmediate = opts?.execImmediate ?? true;
|
|
433
|
+
const data = this.builder.toJSON();
|
|
434
|
+
this.builder.clear();
|
|
435
|
+
const response = {
|
|
436
|
+
type: "ack",
|
|
437
|
+
msgid: this.msgid,
|
|
438
|
+
data
|
|
439
|
+
};
|
|
440
|
+
if (!execImmediate) {
|
|
441
|
+
response.type = "command";
|
|
442
|
+
response.command = "redirect";
|
|
443
|
+
response.queueCommand = true;
|
|
444
|
+
}
|
|
445
|
+
this.wsSend(response);
|
|
446
|
+
}
|
|
447
|
+
// ---------------------------------------------------------------------------
|
|
448
|
+
// Inject commands (immediate execution, bypass verb queue)
|
|
449
|
+
// ---------------------------------------------------------------------------
|
|
450
|
+
/** Send an inject command for immediate execution. */
|
|
451
|
+
injectCommand(command, data, callSid) {
|
|
452
|
+
this.wsSend({
|
|
453
|
+
type: "command",
|
|
454
|
+
command: "inject",
|
|
455
|
+
data: {
|
|
456
|
+
command,
|
|
457
|
+
...typeof data === "object" && data !== null ? data : {},
|
|
458
|
+
...callSid ? { call_sid: callSid } : {}
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
/** Redirect call execution to a new webhook. */
|
|
463
|
+
injectRedirect(hook, callSid) {
|
|
464
|
+
this.injectCommand("redirect", { call_hook: hook }, callSid);
|
|
465
|
+
}
|
|
466
|
+
/** Inject a whisper verb (say/play) to one party on the call. */
|
|
467
|
+
injectWhisper(verb, callSid) {
|
|
468
|
+
this.injectCommand("whisper", Array.isArray(verb) ? verb : [verb], callSid);
|
|
469
|
+
}
|
|
470
|
+
/** Mute or unmute the call. */
|
|
471
|
+
injectMute(status, callSid) {
|
|
472
|
+
this.injectCommand("mute:status", { mute_status: status }, callSid);
|
|
473
|
+
}
|
|
474
|
+
/** Pause or resume audio streaming (listen/stream). */
|
|
475
|
+
injectListenStatus(status, callSid) {
|
|
476
|
+
this.injectCommand("listen:status", { listen_status: status }, callSid);
|
|
477
|
+
}
|
|
478
|
+
/** Control call recording. */
|
|
479
|
+
injectRecord(action, opts, callSid) {
|
|
480
|
+
this.injectCommand("record", { action, ...opts }, callSid);
|
|
481
|
+
}
|
|
482
|
+
/** Send DTMF digits into the call. */
|
|
483
|
+
injectDtmf(digit, duration, callSid) {
|
|
484
|
+
this.injectCommand("dtmf", { digit, ...duration !== void 0 ? { duration } : {} }, callSid);
|
|
485
|
+
}
|
|
486
|
+
/** Attach metadata to the call. */
|
|
487
|
+
injectTag(data, callSid) {
|
|
488
|
+
this.injectCommand("tag", data, callSid);
|
|
489
|
+
}
|
|
490
|
+
/** Send a generic command. */
|
|
491
|
+
sendCommand(command, data) {
|
|
492
|
+
this.wsSend({
|
|
493
|
+
type: "command",
|
|
494
|
+
command,
|
|
495
|
+
data
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
/** Send tool output to an active LLM conversation. */
|
|
499
|
+
sendToolOutput(toolCallId, data) {
|
|
500
|
+
this.wsSend({
|
|
501
|
+
type: "command",
|
|
502
|
+
command: "llm:tool-result",
|
|
503
|
+
data: { tool_call_id: toolCallId, ...typeof data === "object" && data !== null ? data : { result: data } }
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
/** Update an active LLM conversation. */
|
|
507
|
+
updateLlm(data) {
|
|
508
|
+
this.wsSend({
|
|
509
|
+
type: "command",
|
|
510
|
+
command: "llm:update",
|
|
511
|
+
data
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
// ---------------------------------------------------------------------------
|
|
515
|
+
// TTS Token Streaming
|
|
516
|
+
// ---------------------------------------------------------------------------
|
|
517
|
+
/** Whether TTS streaming is paused due to backpressure. */
|
|
518
|
+
get isTtsPaused() {
|
|
519
|
+
return this.ttsPaused;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Send a streaming TTS token.
|
|
523
|
+
* Returns a promise that resolves when jambonz acknowledges receipt.
|
|
524
|
+
* Backpressure is applied automatically: if jambonz signals 'full',
|
|
525
|
+
* subsequent tokens queue until a 'stream_resumed' event arrives.
|
|
526
|
+
*/
|
|
527
|
+
sendTtsTokens(text) {
|
|
528
|
+
return new Promise((resolve2, reject) => {
|
|
529
|
+
const id = ++this.ttsCounter;
|
|
530
|
+
this.commandQueue.push({ type: "tokens", id, tokens: text, resolve: resolve2, reject });
|
|
531
|
+
if (this.commandQueue.length === 1 && !this.ttsPaused) {
|
|
532
|
+
this.processQueue();
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
/** Queue a TTS flush command. Sent immediately if the queue is empty. */
|
|
537
|
+
flushTtsTokens() {
|
|
538
|
+
if (this.commandQueue.length === 0) {
|
|
539
|
+
this.sendCommand("tts:flush");
|
|
540
|
+
} else {
|
|
541
|
+
this.commandQueue.push({
|
|
542
|
+
type: "flush",
|
|
543
|
+
id: ++this.ttsCounter,
|
|
544
|
+
resolve: () => {
|
|
545
|
+
},
|
|
546
|
+
reject: () => {
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/** Clear all pending TTS tokens and send tts:clear. */
|
|
552
|
+
clearTtsTokens() {
|
|
553
|
+
for (const entry of this.commandQueue) {
|
|
554
|
+
if (entry.type === "tokens") {
|
|
555
|
+
entry.resolve();
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
this.commandQueue = [];
|
|
559
|
+
this.ttsPaused = false;
|
|
560
|
+
this.sendCommand("tts:clear");
|
|
561
|
+
}
|
|
562
|
+
processQueue() {
|
|
563
|
+
while (this.commandQueue.length > 0 && this.commandQueue[0].type === "flush") {
|
|
564
|
+
const entry = this.commandQueue.shift();
|
|
565
|
+
this.sendCommand("tts:flush");
|
|
566
|
+
entry.resolve();
|
|
567
|
+
}
|
|
568
|
+
if (this.commandQueue.length > 0 && !this.ttsPaused) {
|
|
569
|
+
const entry = this.commandQueue[0];
|
|
570
|
+
this.wsSend({
|
|
571
|
+
type: "command",
|
|
572
|
+
command: "tts:tokens",
|
|
573
|
+
data: { id: entry.id, tokens: entry.tokens }
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
handleTtsTokensResult(data) {
|
|
578
|
+
if (this.commandQueue.length === 0) return;
|
|
579
|
+
const entry = this.commandQueue[0];
|
|
580
|
+
if (entry.type !== "tokens") return;
|
|
581
|
+
const { status, reason } = data;
|
|
582
|
+
if (status === "ok") {
|
|
583
|
+
this.commandQueue.shift();
|
|
584
|
+
entry.resolve();
|
|
585
|
+
} else if (reason === "full") {
|
|
586
|
+
this.ttsPaused = true;
|
|
587
|
+
entry.resolve();
|
|
588
|
+
} else {
|
|
589
|
+
this.commandQueue.shift();
|
|
590
|
+
entry.reject(new Error(`tts-token error: ${reason}`));
|
|
591
|
+
}
|
|
592
|
+
if (this.commandQueue.length > 0 && !this.ttsPaused) {
|
|
593
|
+
this.processQueue();
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
handleUserInterruption() {
|
|
597
|
+
for (const entry of this.commandQueue) {
|
|
598
|
+
if (entry.type === "tokens") {
|
|
599
|
+
entry.resolve();
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
this.commandQueue = [];
|
|
603
|
+
this.ttsPaused = false;
|
|
604
|
+
this.emit("tts:user_interrupt");
|
|
605
|
+
}
|
|
606
|
+
resumeTtsStreaming() {
|
|
607
|
+
this.ttsPaused = false;
|
|
608
|
+
this.processQueue();
|
|
609
|
+
}
|
|
610
|
+
// ---------------------------------------------------------------------------
|
|
611
|
+
// Inbound message handling
|
|
612
|
+
// ---------------------------------------------------------------------------
|
|
613
|
+
onMessage(raw) {
|
|
614
|
+
try {
|
|
615
|
+
const str = typeof raw === "string" ? raw : Buffer.from(raw).toString("utf-8");
|
|
616
|
+
const msg = JSON.parse(str);
|
|
617
|
+
const { type, msgid, data, hook } = msg;
|
|
618
|
+
switch (type) {
|
|
619
|
+
case "verb:hook":
|
|
620
|
+
case "session:redirect":
|
|
621
|
+
case "dial:confirm":
|
|
622
|
+
this.msgid = msgid;
|
|
623
|
+
if (hook && this.listenerCount(hook) > 0) {
|
|
624
|
+
this.emit(hook, data);
|
|
625
|
+
} else if (type === "verb:hook" && this.listenerCount("verb:hook") > 0) {
|
|
626
|
+
this.emit("verb:hook", hook, data);
|
|
627
|
+
} else {
|
|
628
|
+
this.reply();
|
|
629
|
+
}
|
|
630
|
+
break;
|
|
631
|
+
case "llm:event":
|
|
632
|
+
case "llm:tool-call":
|
|
633
|
+
if (hook) {
|
|
634
|
+
this.emit(hook, data);
|
|
635
|
+
}
|
|
636
|
+
this.emit(type, data);
|
|
637
|
+
break;
|
|
638
|
+
case "tts:streaming-event":
|
|
639
|
+
if (data?.event_type === "stream_resumed") {
|
|
640
|
+
this.resumeTtsStreaming();
|
|
641
|
+
} else if (data?.event_type === "user_interruption") {
|
|
642
|
+
this.handleUserInterruption();
|
|
643
|
+
}
|
|
644
|
+
this.emit("tts:streaming-event", data);
|
|
645
|
+
break;
|
|
646
|
+
case "tts:tokens-result":
|
|
647
|
+
this.handleTtsTokensResult(data ?? {});
|
|
648
|
+
break;
|
|
649
|
+
case "call:status":
|
|
650
|
+
case "verb:status":
|
|
651
|
+
case "jambonz:error":
|
|
652
|
+
this.emit(type, data);
|
|
653
|
+
break;
|
|
654
|
+
default:
|
|
655
|
+
this.logger.debug({ type }, "Session: unhandled message type");
|
|
656
|
+
}
|
|
657
|
+
} catch (err) {
|
|
658
|
+
this.logger.info({ err }, "Session: error handling message");
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
// ---------------------------------------------------------------------------
|
|
662
|
+
// Utilities
|
|
663
|
+
// ---------------------------------------------------------------------------
|
|
664
|
+
wsSend(msg) {
|
|
665
|
+
if (this.ws.readyState === 1) {
|
|
666
|
+
this.ws.send(JSON.stringify(msg));
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/** Close the WebSocket connection. */
|
|
670
|
+
close() {
|
|
671
|
+
this.ws.close();
|
|
672
|
+
}
|
|
673
|
+
toJSON() {
|
|
674
|
+
return this.data;
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
// src/websocket/client.ts
|
|
679
|
+
var WsClient = class extends EventEmitter {
|
|
680
|
+
logger;
|
|
681
|
+
path;
|
|
682
|
+
constructor(logger, path) {
|
|
683
|
+
super();
|
|
684
|
+
this.logger = logger;
|
|
685
|
+
this.path = path;
|
|
686
|
+
}
|
|
687
|
+
/** Handle a new WebSocket connection. Called by the endpoint on upgrade. */
|
|
688
|
+
handle(ws, req) {
|
|
689
|
+
const onMessage = (data) => {
|
|
690
|
+
try {
|
|
691
|
+
const raw = typeof data === "string" ? data : Buffer.from(data).toString("utf-8");
|
|
692
|
+
const msg = JSON.parse(raw);
|
|
693
|
+
this.onSessionMessage(ws, req, msg, onMessage);
|
|
694
|
+
} catch (err) {
|
|
695
|
+
this.logger.info({ err }, "WsClient: error parsing message");
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
ws.on("message", onMessage);
|
|
699
|
+
}
|
|
700
|
+
onSessionMessage(ws, req, msg, onMessage) {
|
|
701
|
+
const { type } = msg;
|
|
702
|
+
if (type === "session:new" || type === "session:adulting" || type === "session:reconnect") {
|
|
703
|
+
ws.removeListener("message", onMessage);
|
|
704
|
+
const session = new Session({ ws, msg, logger: this.logger });
|
|
705
|
+
this.emit("session:new", session, this.path, req);
|
|
706
|
+
} else if (type === "session:redirect") {
|
|
707
|
+
ws.removeListener("message", onMessage);
|
|
708
|
+
const session = new Session({ ws, msg, logger: this.logger });
|
|
709
|
+
if (this.listenerCount("session:redirect") > 0) {
|
|
710
|
+
this.emit("session:redirect", session, this.path, req);
|
|
711
|
+
} else {
|
|
712
|
+
this.emit("session:new", session, this.path, req);
|
|
713
|
+
}
|
|
714
|
+
} else {
|
|
715
|
+
this.logger.debug({ type }, "WsClient: unexpected message type before session");
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
// src/websocket/router.ts
|
|
721
|
+
var WsRouter = class {
|
|
722
|
+
routes = [];
|
|
723
|
+
wildcard = null;
|
|
724
|
+
/** Register a client for a path pattern. Use '*' for wildcard. */
|
|
725
|
+
use(path, client) {
|
|
726
|
+
if (path === "*") {
|
|
727
|
+
this.wildcard = client;
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
const normalized = path.startsWith("/") ? path : `/${path}`;
|
|
731
|
+
const chunks = normalized.split("/").filter(Boolean);
|
|
732
|
+
this.routes.push({ path: normalized, chunks, client });
|
|
733
|
+
this.routes.sort((a, b) => b.chunks.length - a.chunks.length);
|
|
734
|
+
}
|
|
735
|
+
/** Find the matching client for a request. Returns undefined if no match. */
|
|
736
|
+
route(req) {
|
|
737
|
+
const pathname = req.url?.split("?")[0];
|
|
738
|
+
if (!pathname) return this.wildcard ?? void 0;
|
|
739
|
+
const urlChunks = pathname.split("/").filter(Boolean);
|
|
740
|
+
for (const route of this.routes) {
|
|
741
|
+
if (route.chunks.length > urlChunks.length) continue;
|
|
742
|
+
const matched = route.chunks.every((chunk, i) => chunk === urlChunks[i]);
|
|
743
|
+
if (matched) {
|
|
744
|
+
const remaining = "/" + urlChunks.slice(route.chunks.length).join("/");
|
|
745
|
+
req.originalUrl = req.url;
|
|
746
|
+
req.url = remaining;
|
|
747
|
+
return route.client;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
return this.wildcard ?? void 0;
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
// src/websocket/endpoint.ts
|
|
755
|
+
var WS_PROTOCOL = "ws.jambonz.org";
|
|
756
|
+
var defaultLogger = {
|
|
757
|
+
info: () => {
|
|
758
|
+
},
|
|
759
|
+
error: () => {
|
|
760
|
+
},
|
|
761
|
+
debug: () => {
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
function createEndpoint(opts) {
|
|
765
|
+
const { server, port, logger = defaultLogger, middlewares = [] } = opts;
|
|
766
|
+
const router = new WsRouter();
|
|
767
|
+
const wss = new WebSocketServer({
|
|
768
|
+
noServer: true,
|
|
769
|
+
handleProtocols: (protocols) => {
|
|
770
|
+
if (protocols.has(WS_PROTOCOL)) return WS_PROTOCOL;
|
|
771
|
+
return false;
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
server.on("upgrade", (req, socket, head) => {
|
|
775
|
+
const fakeRes = new http.ServerResponse(req);
|
|
776
|
+
runMiddlewares(middlewares, req, fakeRes, (err) => {
|
|
777
|
+
if (err) {
|
|
778
|
+
logger.info({ err }, "Endpoint: middleware rejected upgrade");
|
|
779
|
+
socket.destroy();
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
const client = router.route(req);
|
|
783
|
+
if (!client) {
|
|
784
|
+
logger.debug({ url: req.url }, "Endpoint: no route for WebSocket upgrade");
|
|
785
|
+
socket.destroy();
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
789
|
+
client.handle(ws, req);
|
|
790
|
+
});
|
|
791
|
+
});
|
|
792
|
+
});
|
|
793
|
+
if (port && !server.listening) {
|
|
794
|
+
server.listen(port);
|
|
795
|
+
}
|
|
796
|
+
return function makeService({ path }) {
|
|
797
|
+
const client = new WsClient(logger, path);
|
|
798
|
+
router.use(path, client);
|
|
799
|
+
return client;
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
function runMiddlewares(middlewares, req, res, done) {
|
|
803
|
+
let i = 0;
|
|
804
|
+
function next(err) {
|
|
805
|
+
if (err) return done(err);
|
|
806
|
+
if (i >= middlewares.length) return done();
|
|
807
|
+
const mw = middlewares[i++];
|
|
808
|
+
try {
|
|
809
|
+
const result = mw(req, res, next);
|
|
810
|
+
if (result instanceof Promise) {
|
|
811
|
+
result.catch(next);
|
|
812
|
+
}
|
|
813
|
+
} catch (e) {
|
|
814
|
+
next(e instanceof Error ? e : new Error(String(e)));
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
next();
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
export { Session, WsClient, WsRouter, createEndpoint };
|
|
821
|
+
//# sourceMappingURL=index.js.map
|
|
822
|
+
//# sourceMappingURL=index.js.map
|