@fonoster/apiserver 0.15.21 → 0.16.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.
Files changed (38) hide show
  1. package/dist/applications/createGetFnUtil.d.ts +3 -3
  2. package/dist/core/httpBridge.js +0 -26
  3. package/dist/voice/VoiceDispatcher.js +1 -0
  4. package/dist/voice/client/AudioSocketHandler.d.ts +35 -0
  5. package/dist/voice/client/AudioSocketHandler.js +68 -0
  6. package/dist/voice/client/AuthorizationHandler.d.ts +13 -0
  7. package/dist/voice/client/AuthorizationHandler.js +78 -0
  8. package/dist/voice/client/ExternalMediaHandler.d.ts +32 -0
  9. package/dist/voice/client/ExternalMediaHandler.js +59 -0
  10. package/dist/voice/client/GrpcClientHandler.d.ts +36 -0
  11. package/dist/voice/client/GrpcClientHandler.js +101 -0
  12. package/dist/voice/client/SpeechHandler.d.ts +60 -0
  13. package/dist/voice/client/SpeechHandler.js +116 -0
  14. package/dist/voice/{VoiceClientImpl.d.ts → client/VoiceClientImpl.d.ts} +17 -17
  15. package/dist/voice/client/VoiceClientImpl.js +158 -0
  16. package/dist/voice/client/index.d.ts +24 -0
  17. package/dist/voice/client/index.js +40 -0
  18. package/dist/voice/connectToAri.js +1 -1
  19. package/dist/voice/createCreateVoiceClient.d.ts +1 -1
  20. package/dist/voice/createCreateVoiceClient.js +3 -3
  21. package/dist/voice/handlers/createSayHandler.js +2 -15
  22. package/dist/voice/handlers/createStopSayHandler.d.ts +3 -0
  23. package/dist/voice/handlers/createStopSayHandler.js +34 -0
  24. package/dist/voice/handlers/index.d.ts +1 -0
  25. package/dist/voice/handlers/index.js +1 -0
  26. package/dist/voice/tts/Azure.js +1 -1
  27. package/dist/voice/tts/Deepgram.d.ts +1 -1
  28. package/dist/voice/tts/Deepgram.js +1 -1
  29. package/dist/voice/tts/ElevenLabs.js +45 -13
  30. package/dist/voice/tts/Google.d.ts +1 -1
  31. package/dist/voice/tts/Google.js +1 -1
  32. package/dist/voice/tts/utils/convertUlawToPCM16.d.ts +26 -0
  33. package/dist/voice/tts/utils/convertUlawToPCM16.js +40 -0
  34. package/dist/voice/tts/utils/index.d.ts +23 -0
  35. package/dist/voice/tts/utils/index.js +39 -0
  36. package/dist/voice/types/voice.d.ts +1 -0
  37. package/package.json +11 -10
  38. package/dist/voice/VoiceClientImpl.js +0 -281
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /**
18
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
19
+ * http://github.com/fonoster/fonoster
20
+ *
21
+ * This file is part of Fonoster
22
+ *
23
+ * Licensed under the MIT License (the "License");
24
+ * you may not use this file except in compliance with
25
+ * the License. You may obtain a copy of the License at
26
+ *
27
+ * https://opensource.org/licenses/MIT
28
+ *
29
+ * Unless required by applicable law or agreed to in writing, software
30
+ * distributed under the License is distributed on an "AS IS" BASIS,
31
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
+ * See the License for the specific language governing permissions and
33
+ * limitations under the License.
34
+ */
35
+ __exportStar(require("./createChunkedSynthesisStream"), exports);
36
+ __exportStar(require("./createErrorStream"), exports);
37
+ __exportStar(require("./isSsml"), exports);
38
+ __exportStar(require("./streamToBuffer"), exports);
39
+ __exportStar(require("./convertUlawToPCM16"), exports);
@@ -46,6 +46,7 @@ type VoiceClient = {
46
46
  digits: string;
47
47
  }>;
48
48
  getTranscriptionsStream: () => Stream;
49
+ stopSynthesis: () => Promise<void>;
49
50
  };
50
51
  type TextToSpeech = {
51
52
  synthesize: (text: string, options: Record<string, unknown>) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/apiserver",
3
- "version": "0.15.21",
3
+ "version": "0.16.0",
4
4
  "description": "APIServer for Fonoster",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/fonoster#readme",
@@ -21,14 +21,14 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@deepgram/sdk": "^3.5.1",
24
- "@fonoster/authz": "^0.15.21",
25
- "@fonoster/autopilot": "^0.15.21",
26
- "@fonoster/common": "^0.15.21",
27
- "@fonoster/identity": "^0.15.21",
28
- "@fonoster/logger": "^0.15.20",
29
- "@fonoster/sipnet": "^0.15.21",
30
- "@fonoster/streams": "^0.15.20",
31
- "@fonoster/types": "^0.15.21",
24
+ "@fonoster/authz": "^0.16.0",
25
+ "@fonoster/autopilot": "^0.16.0",
26
+ "@fonoster/common": "^0.16.0",
27
+ "@fonoster/identity": "^0.16.0",
28
+ "@fonoster/logger": "^0.16.0",
29
+ "@fonoster/sipnet": "^0.16.0",
30
+ "@fonoster/streams": "^0.16.0",
31
+ "@fonoster/types": "^0.16.0",
32
32
  "@google-cloud/speech": "^6.6.0",
33
33
  "@google-cloud/text-to-speech": "^5.3.0",
34
34
  "@grpc/grpc-js": "~1.10.6",
@@ -52,6 +52,7 @@
52
52
  "uuid": "^11.0.3",
53
53
  "validator": "^13.12.0",
54
54
  "wait-port": "^1.1.0",
55
+ "wavefile": "^11.0.0",
55
56
  "zod": "^3.23.8"
56
57
  },
57
58
  "files": [
@@ -74,5 +75,5 @@
74
75
  "@types/uuid": "^10.0.0",
75
76
  "@types/validator": "^13.12.0"
76
77
  },
77
- "gitHead": "12d1215cf30b3b1bfd5b115a7bdfe2c14a6955b6"
78
+ "gitHead": "ad4212a7938ad2e219b9d1ae27a430ec60d9f841"
78
79
  }
@@ -1,281 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
- step((generator = generator.apply(thisArg, _arguments || [])).next());
42
- });
43
- };
44
- Object.defineProperty(exports, "__esModule", { value: true });
45
- exports.VoiceClientImpl = void 0;
46
- /**
47
- * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
48
- * http://github.com/fonoster/fonoster
49
- *
50
- * This file is part of Fonoster
51
- *
52
- * Licensed under the MIT License (the "License");
53
- * you may not use this file except in compliance with
54
- * the License. You may obtain a copy of the License at
55
- *
56
- * https://opensource.org/licenses/MIT
57
- *
58
- * Unless required by applicable law or agreed to in writing, software
59
- * distributed under the License is distributed on an "AS IS" BASIS,
60
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61
- * See the License for the specific language governing permissions and
62
- * limitations under the License.
63
- */
64
- const stream_1 = require("stream");
65
- const authz_1 = require("@fonoster/authz");
66
- const common_1 = require("@fonoster/common");
67
- const logger_1 = require("@fonoster/logger");
68
- const streams_1 = require("@fonoster/streams");
69
- const grpc = __importStar(require("@grpc/grpc-js"));
70
- const pick_port_1 = require("pick-port");
71
- const envs_1 = require("../envs");
72
- const transcribeOnConnection_1 = require("./transcribeOnConnection");
73
- const types_1 = require("./types");
74
- const createExternalMediaConfig_1 = require("./utils/createExternalMediaConfig");
75
- const VoiceServiceClientConstructor_1 = require("./utils/VoiceServiceClientConstructor");
76
- const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
77
- class VoiceClientImpl {
78
- constructor(params, filesServer) {
79
- const { config, tts, stt, ari } = params;
80
- this.config = config;
81
- this.verbsStream = new stream_1.Stream();
82
- this.transcriptionsStream = new stream_1.Stream();
83
- this.tts = tts;
84
- this.stt = stt;
85
- this.ari = ari;
86
- this.filesServer = filesServer;
87
- }
88
- connect() {
89
- return __awaiter(this, void 0, void 0, function* () {
90
- if (envs_1.AUTHZ_SERVICE_ENABLED) {
91
- const { sessionRef: channelId } = this.config;
92
- const { ari } = this;
93
- try {
94
- const authz = new authz_1.AuthzClient(`${envs_1.AUTHZ_SERVICE_HOST}:${envs_1.AUTHZ_SERVICE_PORT}`);
95
- const authorized = yield authz.checkSessionAuthorized({
96
- accessKeyId: this.config.accessKeyId
97
- });
98
- if (!authorized) {
99
- logger.verbose("rejected unauthorized session", { channelId });
100
- yield ari.channels.answer({ channelId });
101
- yield ari.channels.play({ channelId, media: "sound:unavailable" });
102
- yield new Promise((resolve) => setTimeout(resolve, 2000));
103
- yield ari.channels.hangup({ channelId });
104
- return;
105
- }
106
- }
107
- catch (e) {
108
- logger.error("authz service error", e);
109
- yield ari.channels.answer({ channelId });
110
- yield ari.channels.play({ channelId, media: "sound:unavailable" });
111
- yield new Promise((resolve) => setTimeout(resolve, 2000));
112
- yield ari.channels.hangup({ channelId });
113
- return;
114
- }
115
- }
116
- this.grpcClient = new VoiceServiceClientConstructor_1.VoiceServiceClientConstructor(this.config.endpoint, grpc.credentials.createInsecure());
117
- const metadata = new grpc.Metadata();
118
- metadata.add("accessKeyId", this.config.accessKeyId);
119
- metadata.add("token", this.config.sessionToken);
120
- this.voice = this.grpcClient.createSession(metadata);
121
- this.voice.on(common_1.StreamEvent.DATA, (data) => {
122
- this.verbsStream.emit(data.content, data);
123
- });
124
- this.voice.write({ request: this.config });
125
- this.voice.on(common_1.StreamEvent.ERROR, (error) => {
126
- if (error.code === grpc.status.UNAVAILABLE) {
127
- // FIXME: This error should be sent back to the user
128
- logger.error(`voice server not available at "${this.config.endpoint}"`);
129
- return;
130
- }
131
- logger.error(error.message);
132
- });
133
- const externalMediaPort = yield (0, pick_port_1.pickPort)({ type: "tcp" });
134
- this.setupAudioSocket(externalMediaPort);
135
- yield this.setupExternalMedia(externalMediaPort);
136
- });
137
- }
138
- setupAudioSocket(port) {
139
- this.audioSocket = new streams_1.AudioSocket();
140
- this.audioSocket.onConnection((0, transcribeOnConnection_1.transcribeOnConnection)(this.transcriptionsStream));
141
- this.audioSocket.listen(port, () => {
142
- logger.verbose("starting audio socket for voice client", {
143
- port,
144
- appRef: this.config.appRef
145
- });
146
- });
147
- }
148
- on(type, callback) {
149
- this.verbsStream.on(type.toString(), (data) => {
150
- callback(data[type]);
151
- });
152
- }
153
- sendResponse(response) {
154
- this.voice.write(response);
155
- }
156
- getTranscriptionsStream() {
157
- return this.transcriptionsStream;
158
- }
159
- setupExternalMedia(port) {
160
- return __awaiter(this, void 0, void 0, function* () {
161
- // Snoop from the main channel
162
- const snoopChannel = yield this.ari.channels.snoopChannel({
163
- app: common_1.STASIS_APP_NAME,
164
- channelId: this.config.sessionRef,
165
- snoopId: `snoop-${this.config.sessionRef}`,
166
- spy: "in"
167
- });
168
- const bridge = this.ari.Bridge();
169
- yield bridge.create({ type: "mixing" });
170
- this.bridge = bridge;
171
- const channel = this.ari.Channel();
172
- channel.externalMedia((0, createExternalMediaConfig_1.createExternalMediaConfig)(port));
173
- channel.once(types_1.AriEvent.STASIS_START, (_, channel) => __awaiter(this, void 0, void 0, function* () {
174
- bridge.addChannel({ channel: [snoopChannel.id, channel.id] });
175
- }));
176
- channel.once("ChannelLeftBridge", () => __awaiter(this, void 0, void 0, function* () {
177
- try {
178
- yield bridge.destroy();
179
- }
180
- catch (e) {
181
- // We can only try
182
- }
183
- }));
184
- });
185
- }
186
- synthesize(text, options) {
187
- return __awaiter(this, void 0, void 0, function* () {
188
- const { ref, stream } = this.tts.synthesize(text, options);
189
- stream.on("error", (error) => __awaiter(this, void 0, void 0, function* () {
190
- logger.error(`stream error for ref ${ref}: ${error.message}`, {
191
- errorDetails: error.stack || "No stack trace"
192
- });
193
- this.filesServer.removeStream(ref);
194
- }));
195
- this.filesServer.addStream(ref, stream);
196
- return ref;
197
- });
198
- }
199
- transcribe() {
200
- return __awaiter(this, void 0, void 0, function* () {
201
- try {
202
- return yield this.stt.transcribe(this.transcriptionsStream);
203
- }
204
- catch (e) {
205
- logger.warn("transcription error", e);
206
- return {};
207
- }
208
- });
209
- }
210
- startSpeechGather(callback) {
211
- const out = this.stt.streamTranscribe(this.transcriptionsStream);
212
- out.on("data", callback);
213
- out.on("error", (error) => __awaiter(this, void 0, void 0, function* () {
214
- logger.error("speech recognition error", { error });
215
- const { sessionRef: channelId } = this.config;
216
- const { ari } = this;
217
- ari.channels.hangup({ channelId });
218
- }));
219
- }
220
- startDtmfGather(sessionRef, callback) {
221
- return __awaiter(this, void 0, void 0, function* () {
222
- const channel = yield this.ari.channels.get({ channelId: sessionRef });
223
- channel.on(types_1.AriEvent.CHANNEL_DTMF_RECEIVED, (event) => {
224
- const { digit } = event;
225
- callback({ digit });
226
- });
227
- });
228
- }
229
- // Stops both speech and dtmf gather
230
- stopStreamGather() {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- throw new Error("Method 'stopStreamGather' not implemented.");
233
- });
234
- }
235
- waitForDtmf(params) {
236
- return __awaiter(this, void 0, void 0, function* () {
237
- const { onDigitReceived, sessionRef, finishOnKey, maxDigits, timeout } = params;
238
- let result = "";
239
- let timeoutId = null;
240
- const channel = yield this.ari.channels.get({ channelId: sessionRef });
241
- return new Promise((resolve) => {
242
- const resetTimer = () => {
243
- if (timeoutId) {
244
- clearTimeout(timeoutId);
245
- }
246
- timeoutId = setTimeout(() => {
247
- channel.removeListener(types_1.AriEvent.CHANNEL_DTMF_RECEIVED, dtmfListener);
248
- resolve({ digits: result });
249
- }, timeout);
250
- };
251
- const dtmfListener = (event) => {
252
- const { digit } = event;
253
- // Stops the global timeout
254
- onDigitReceived();
255
- resetTimer();
256
- if (digit !== finishOnKey) {
257
- result += digit;
258
- }
259
- if (result.length >= maxDigits || digit === finishOnKey) {
260
- clearTimeout(timeoutId);
261
- channel.removeListener(types_1.AriEvent.CHANNEL_DTMF_RECEIVED, dtmfListener);
262
- resolve({ digits: result });
263
- }
264
- };
265
- channel.on(types_1.AriEvent.CHANNEL_DTMF_RECEIVED, dtmfListener);
266
- resetTimer(); // Start the initial timeout
267
- });
268
- });
269
- }
270
- close() {
271
- try {
272
- this.voice.end();
273
- this.grpcClient.close();
274
- this.audioSocket.close();
275
- }
276
- catch (e) {
277
- // Do nothing
278
- }
279
- }
280
- }
281
- exports.VoiceClientImpl = VoiceClientImpl;