@fonoster/apiserver 0.7.1 → 0.7.2

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.
@@ -23,7 +23,7 @@ declare function createGetFnUtil(prisma: Prisma): (ref: string) => Promise<{
23
23
  productRef: string;
24
24
  };
25
25
  name: string;
26
- type: "PROGRAMMABLE_VOICE";
26
+ type: "EXTERNAL";
27
27
  ref: string;
28
28
  createdAt: Date;
29
29
  updatedAt: Date;
@@ -4,7 +4,7 @@ exports.convertToApplicationData = convertToApplicationData;
4
4
  const client_1 = require("@prisma/client");
5
5
  const pb_util_1 = require("pb-util");
6
6
  function convertToApplicationData(request) {
7
- const type = request.type || client_1.ApplicationType.PROGRAMMABLE_VOICE;
7
+ const type = request.type || client_1.ApplicationType.EXTERNAL;
8
8
  const result = {
9
9
  ref: request.ref, // Only for UpdateApplicationRequest
10
10
  name: request.name,
@@ -5,7 +5,7 @@ declare function getApplicationValidationSchema(request: {
5
5
  }): z.ZodObject<{
6
6
  name: z.ZodString;
7
7
  type: z.ZodNativeEnum<{
8
- PROGRAMMABLE_VOICE: "PROGRAMMABLE_VOICE";
8
+ EXTERNAL: "EXTERNAL";
9
9
  }>;
10
10
  endpoint: z.ZodEffects<z.ZodNullable<z.ZodOptional<z.ZodString>>, string, string>;
11
11
  textToSpeech: z.ZodUndefined | z.ZodObject<{
@@ -34,7 +34,7 @@ declare function getApplicationValidationSchema(request: {
34
34
  }>;
35
35
  }, "strip", z.ZodTypeAny, {
36
36
  name?: string;
37
- type?: "PROGRAMMABLE_VOICE";
37
+ type?: "EXTERNAL";
38
38
  endpoint?: string;
39
39
  textToSpeech?: {
40
40
  [x: string]: any;
@@ -48,7 +48,7 @@ declare function getApplicationValidationSchema(request: {
48
48
  };
49
49
  }, {
50
50
  name?: string;
51
- type?: "PROGRAMMABLE_VOICE";
51
+ type?: "EXTERNAL";
52
52
  endpoint?: string;
53
53
  textToSpeech?: {
54
54
  [x: string]: any;
@@ -4,7 +4,7 @@ exports.prepareForValidation = prepareForValidation;
4
4
  const client_1 = require("@prisma/client");
5
5
  const pb_util_1 = require("pb-util");
6
6
  function prepareForValidation(request) {
7
- const type = request.type || client_1.ApplicationType.PROGRAMMABLE_VOICE;
7
+ const type = request.type || client_1.ApplicationType.EXTERNAL;
8
8
  const result = {
9
9
  ref: request.ref, // Only for UpdateApplicationRequest
10
10
  name: request.name,
@@ -1,7 +1,7 @@
1
1
  import { Stream } from "stream";
2
2
  import { SayOptions, VoiceClientConfig, VoiceIn, VoiceSessionStreamClient } from "@fonoster/common";
3
3
  import { AudioSocket } from "@fonoster/streams";
4
- import { Client } from "ari-client";
4
+ import { Bridge, Client } from "ari-client";
5
5
  import { SpeechResult } from "./stt/types";
6
6
  import { GRPCClient, SpeechToText, TextToSpeech, VoiceClient } from "./types";
7
7
  declare class VoiceClientImpl implements VoiceClient {
@@ -15,6 +15,7 @@ declare class VoiceClientImpl implements VoiceClient {
15
15
  audioSocket: AudioSocket;
16
16
  asStream: Stream;
17
17
  ari: Client;
18
+ bridge: Bridge;
18
19
  constructor(params: {
19
20
  ari: Client;
20
21
  config: VoiceClientConfig;
@@ -23,9 +24,11 @@ declare class VoiceClientImpl implements VoiceClient {
23
24
  });
24
25
  connect(): Promise<void>;
25
26
  setupAudioSocket(port: number): void;
26
- setupExternalMedia(port: number): Promise<void>;
27
27
  on(type: string, callback: (data: VoiceIn) => void): void;
28
28
  sendResponse(response: VoiceIn): void;
29
+ getBridge(): Bridge;
30
+ getTranscriptionsStream(): Stream;
31
+ setupExternalMedia(port: number): Promise<void>;
29
32
  synthesize(text: string, options: SayOptions): Promise<string>;
30
33
  transcribe(): Promise<SpeechResult>;
31
34
  startSpeechGather(callback: (stream: {
@@ -107,17 +107,36 @@ class VoiceClientImpl {
107
107
  });
108
108
  });
109
109
  }
110
+ on(type, callback) {
111
+ this.verbsStream.on(type.toString(), (data) => {
112
+ callback(data[type]);
113
+ });
114
+ }
115
+ sendResponse(response) {
116
+ this.voice.write(response);
117
+ }
118
+ getBridge() {
119
+ return this.bridge;
120
+ }
121
+ getTranscriptionsStream() {
122
+ return this.transcriptionsStream;
123
+ }
110
124
  setupExternalMedia(port) {
111
125
  return __awaiter(this, void 0, void 0, function* () {
126
+ // Snoop from the main channel
127
+ const snoopChannel = yield this.ari.channels.snoopChannel({
128
+ app: common_1.STASIS_APP_NAME,
129
+ channelId: this.config.sessionRef,
130
+ snoopId: "snoop-" + this.config.sessionRef,
131
+ spy: "in"
132
+ });
112
133
  const bridge = this.ari.Bridge();
113
134
  yield bridge.create({ type: "mixing" });
135
+ this.bridge = bridge;
114
136
  const channel = this.ari.Channel();
115
137
  channel.externalMedia((0, createExternalMediaConfig_1.createExternalMediaConfig)(port));
116
138
  channel.once(types_1.AriEvent.STASIS_START, (_, channel) => __awaiter(this, void 0, void 0, function* () {
117
- yield bridge.addChannel({
118
- channel: [this.config.sessionRef, channel.id]
119
- });
120
- yield channel.answer();
139
+ bridge.addChannel({ channel: [snoopChannel.id, channel.id] });
121
140
  }));
122
141
  channel.once("ChannelLeftBridge", () => __awaiter(this, void 0, void 0, function* () {
123
142
  // TODO: Evaluate a better way to handle this
@@ -133,14 +152,6 @@ class VoiceClientImpl {
133
152
  }));
134
153
  });
135
154
  }
136
- on(type, callback) {
137
- this.verbsStream.on(type.toString(), (data) => {
138
- callback(data[type]);
139
- });
140
- }
141
- sendResponse(response) {
142
- this.voice.write(response);
143
- }
144
155
  synthesize(text, options) {
145
156
  return __awaiter(this, void 0, void 0, function* () {
146
157
  return yield this.tts.synthesize(text, options);
@@ -31,6 +31,7 @@ exports.VoiceDispatcher = void 0;
31
31
  const common_1 = require("@fonoster/common");
32
32
  const logger_1 = require("@fonoster/logger");
33
33
  const handlers_1 = require("./handlers");
34
+ const Stream_1 = require("./handlers/Stream");
34
35
  const makeGetChannelVar_1 = require("./makeGetChannelVar");
35
36
  const types_1 = require("./types");
36
37
  const utils_1 = require("../utils");
@@ -83,6 +84,7 @@ class VoiceDispatcher {
83
84
  vc.on(common_1.StreamContent.STOP_STREAM_GATHER_REQUEST, () => {
84
85
  vc.stopStreamGather();
85
86
  });
87
+ vc.on(common_1.StreamContent.START_STREAM_REQUEST, (0, Stream_1.streamHandler)(vc).bind(this));
86
88
  }
87
89
  catch (err) {
88
90
  logger.error("error handling stasis start", { error: err.message });
@@ -10,14 +10,47 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.playbackControlHandler = playbackControlHandler;
13
+ /*
14
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const common_1 = require("@fonoster/common");
32
+ const zod_1 = require("zod");
13
33
  const withErrorHandling_1 = require("./utils/withErrorHandling");
34
+ const requestSchema = zod_1.z.object({
35
+ sessionRef: zod_1.z.string(),
36
+ playbackRef: zod_1.z.string().optional().nullable(),
37
+ action: zod_1.z.nativeEnum(common_1.PlaybackControlAction)
38
+ });
14
39
  function playbackControlHandler(ari, voiceClient) {
15
40
  return (0, withErrorHandling_1.withErrorHandling)((playbackControlReq) => __awaiter(this, void 0, void 0, function* () {
16
- const { sessionRef, playbackRef, action } = playbackControlReq;
17
- yield ari.playbacks.control({
18
- playbackId: playbackRef,
19
- operation: action
20
- });
41
+ requestSchema.parse(playbackControlReq);
42
+ const { sessionRef, playbackRef: playbackId, action } = playbackControlReq;
43
+ try {
44
+ if (action === common_1.PlaybackControlAction.STOP) {
45
+ yield ari.playbacks.stop({ playbackId });
46
+ }
47
+ else {
48
+ yield ari.playbacks.control({ playbackId, operation: action });
49
+ }
50
+ }
51
+ catch (err) {
52
+ // Ignore error
53
+ }
21
54
  voiceClient.sendResponse({
22
55
  playbackControlResponse: {
23
56
  sessionRef
@@ -25,13 +25,13 @@ const sayRequestSchema = zod_1.z.object({
25
25
  const getMediaUrl = (filename) => `sound:http://${envs_1.APISERVER_HOST}:${envs_1.FILES_SERVER_PORT}/sounds/${filename}.sln16`;
26
26
  function sayHandler(ari, voiceClient) {
27
27
  return (0, withErrorHandling_1.withErrorHandling)((request) => __awaiter(this, void 0, void 0, function* () {
28
- const { sessionRef } = request;
28
+ const { sessionRef: channelId } = request;
29
29
  // Error handled by withErrorHandling
30
30
  sayRequestSchema.parse(request);
31
31
  const playbackRef = request.playbackRef || (0, nanoid_1.nanoid)(10);
32
32
  const filename = yield voiceClient.synthesize(request.text, request.options ? pb_util_1.struct.decode(request.options) : {});
33
33
  yield ari.channels.play({
34
- channelId: sessionRef,
34
+ channelId,
35
35
  media: getMediaUrl(filename),
36
36
  playbackId: playbackRef
37
37
  });
@@ -0,0 +1,3 @@
1
+ import { VoiceClient } from "../types";
2
+ declare function streamHandler(voiceClient: VoiceClient): (request: import("@fonoster/common").VerbRequest) => Promise<void>;
3
+ export { streamHandler };
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.streamHandler = streamHandler;
13
+ /*
14
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const common_1 = require("@fonoster/common");
32
+ const zod_1 = require("zod");
33
+ const withErrorHandling_1 = require("./utils/withErrorHandling");
34
+ const streamRequestSchema = zod_1.z.object({
35
+ direction: zod_1.z.nativeEnum(common_1.StreamDirection).optional(),
36
+ format: zod_1.z.nativeEnum(common_1.StreamAudioFormat).optional()
37
+ });
38
+ function streamHandler(voiceClient) {
39
+ return (0, withErrorHandling_1.withErrorHandling)((request) => __awaiter(this, void 0, void 0, function* () {
40
+ const { sessionRef, direction, format } = request;
41
+ // Error handled by withErrorHandling
42
+ streamRequestSchema.parse(request);
43
+ const effectiveDirection = direction || common_1.StreamDirection.BOTH;
44
+ const effectiveFormat = format || common_1.StreamAudioFormat.WAV;
45
+ // FIXME: Implement stream IN and correct streamRef
46
+ if (effectiveDirection.includes(common_1.StreamDirection.OUT) ||
47
+ effectiveDirection === common_1.StreamDirection.BOTH) {
48
+ voiceClient.getTranscriptionsStream().on("data", (data) => {
49
+ voiceClient.sendResponse({
50
+ streamPayload: {
51
+ sessionRef,
52
+ type: common_1.StreamMessageType.AUDIO_OUT,
53
+ data,
54
+ streamRef: "fixme",
55
+ format: effectiveFormat
56
+ }
57
+ });
58
+ });
59
+ }
60
+ voiceClient.sendResponse({
61
+ startStreamResponse: {
62
+ sessionRef,
63
+ streamRef: "fixme"
64
+ }
65
+ });
66
+ }));
67
+ }
@@ -62,5 +62,10 @@ function streamGatherHandler(voiceClient) {
62
62
  });
63
63
  });
64
64
  }
65
+ voiceClient.sendResponse({
66
+ startStreamGatherResponse: {
67
+ sessionRef
68
+ }
69
+ });
65
70
  }));
66
71
  }
@@ -73,13 +73,6 @@ class Deepgram extends AbstractSpeechToText_1.AbstractSpeechToText {
73
73
  const connection = this.client.listen.live(buildTranscribeConfig(this.engineConfig.config));
74
74
  const out = new stream_1.Stream();
75
75
  connection.on(LiveTranscriptionEvents.Open, () => {
76
- // WARNING: This is a workaround to keep the connection open while the system
77
- // is playing a file which causes the stream to be paused. We need to look into why
78
- // the stream is being paused.
79
- const keepAliveInterval = setInterval(() => {
80
- const keepAliveMsg = JSON.stringify({ type: "KeepAlive" });
81
- connection.send(keepAliveMsg);
82
- }, 3000);
83
76
  stream.on("data", (chunk) => {
84
77
  connection.send(chunk);
85
78
  });
@@ -93,7 +86,6 @@ class Deepgram extends AbstractSpeechToText_1.AbstractSpeechToText {
93
86
  });
94
87
  connection.on(LiveTranscriptionEvents.Error, (err) => {
95
88
  logger.warn("error on Deepgram connection", { err });
96
- clearInterval(keepAliveInterval);
97
89
  connection.destroy();
98
90
  });
99
91
  });
@@ -86,7 +86,8 @@ class Deepgram extends AbstractTextToSpeech_1.AbstractTextToSpeech {
86
86
  const response = yield this.client.speak.request({ text }, {
87
87
  model: voice || common_1.DeepgramVoice.AURA_ASTERIA_EN,
88
88
  encoding: this.AUDIO_ENCODING,
89
- sample_rate: this.SAMPLE_RATE_HERTZ
89
+ sample_rate: this.SAMPLE_RATE_HERTZ,
90
+ container: "none"
90
91
  });
91
92
  const writeFile = util.promisify(fs.writeFile);
92
93
  const audioBuffer = yield getAudioBuffer(yield response.getStream());
@@ -1,6 +1,7 @@
1
1
  import { Stream } from "stream";
2
2
  import { SayOptions, StreamContent, VoiceClientConfig, VoiceIn, VoiceSessionStreamClient } from "@fonoster/common";
3
3
  import * as grpc from "@grpc/grpc-js";
4
+ import { Bridge } from "ari-client";
4
5
  import { SpeechResult, StreamSpeech } from "../stt/types";
5
6
  type VoiceClient = {
6
7
  config: VoiceClientConfig;
@@ -26,6 +27,8 @@ type VoiceClient = {
26
27
  }) => Promise<{
27
28
  digits: string;
28
29
  }>;
30
+ getBridge: () => Bridge;
31
+ getTranscriptionsStream: () => Stream;
29
32
  };
30
33
  type TextToSpeech = {
31
34
  synthesize: (text: string, options: Record<string, unknown>) => Promise<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/apiserver",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "APIServer for Fonoster",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/fonoster#readme",
@@ -21,12 +21,12 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@deepgram/sdk": "^3.5.1",
24
- "@fonoster/common": "^0.7.0",
25
- "@fonoster/identity": "^0.7.0",
26
- "@fonoster/logger": "^0.7.0",
27
- "@fonoster/sipnet": "^0.7.0",
28
- "@fonoster/streams": "^0.7.0",
29
- "@fonoster/types": "^0.7.0",
24
+ "@fonoster/common": "^0.7.2",
25
+ "@fonoster/identity": "^0.7.2",
26
+ "@fonoster/logger": "^0.7.2",
27
+ "@fonoster/sipnet": "^0.7.2",
28
+ "@fonoster/streams": "^0.7.2",
29
+ "@fonoster/types": "^0.7.2",
30
30
  "@google-cloud/speech": "^6.6.0",
31
31
  "@google-cloud/text-to-speech": "^5.3.0",
32
32
  "@grpc/grpc-js": "~1.10.6",
@@ -71,5 +71,5 @@
71
71
  "@types/uuid": "^9.0.8",
72
72
  "@types/validator": "^13.12.0"
73
73
  },
74
- "gitHead": "06f57a4f909fdc3c81d895ae0b25747bc7ef6c91"
74
+ "gitHead": "6d858d9920132dfef0dba8965e5005f44de61563"
75
75
  }