@deepgram/sdk 3.5.1 → 3.7.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/README.md +22 -0
- package/dist/main/DeepgramClient.d.ts +7 -1
- package/dist/main/DeepgramClient.d.ts.map +1 -1
- package/dist/main/DeepgramClient.js +8 -0
- package/dist/main/DeepgramClient.js.map +1 -1
- package/dist/main/lib/enums/LiveTTSEvents.d.ts +34 -0
- package/dist/main/lib/enums/LiveTTSEvents.d.ts.map +1 -0
- package/dist/main/lib/enums/LiveTTSEvents.js +38 -0
- package/dist/main/lib/enums/LiveTTSEvents.js.map +1 -0
- package/dist/main/lib/enums/index.d.ts +1 -0
- package/dist/main/lib/enums/index.d.ts.map +1 -1
- package/dist/main/lib/enums/index.js +1 -0
- package/dist/main/lib/enums/index.js.map +1 -1
- package/dist/main/lib/types/GetModelsResponse.d.ts +24 -0
- package/dist/main/lib/types/GetModelsResponse.d.ts.map +1 -0
- package/dist/main/lib/types/GetModelsResponse.js +3 -0
- package/dist/main/lib/types/GetModelsResponse.js.map +1 -0
- package/dist/main/lib/types/GetModelsSchema.d.ts +4 -0
- package/dist/main/lib/types/GetModelsSchema.d.ts.map +1 -0
- package/dist/main/lib/types/GetModelsSchema.js +3 -0
- package/dist/main/lib/types/GetModelsSchema.js.map +1 -0
- package/dist/main/lib/types/index.d.ts +2 -0
- package/dist/main/lib/types/index.d.ts.map +1 -1
- package/dist/main/lib/types/index.js +2 -0
- package/dist/main/lib/types/index.js.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/main/packages/ManageRestClient.d.ts +44 -1
- package/dist/main/packages/ManageRestClient.d.ts.map +1 -1
- package/dist/main/packages/ManageRestClient.js +71 -0
- package/dist/main/packages/ManageRestClient.js.map +1 -1
- package/dist/main/packages/ModelsRestClient.d.ts +53 -0
- package/dist/main/packages/ModelsRestClient.d.ts.map +1 -0
- package/dist/main/packages/ModelsRestClient.js +97 -0
- package/dist/main/packages/ModelsRestClient.js.map +1 -0
- package/dist/main/packages/SpeakClient.d.ts +27 -0
- package/dist/main/packages/SpeakClient.d.ts.map +1 -0
- package/dist/main/packages/SpeakClient.js +37 -0
- package/dist/main/packages/SpeakClient.js.map +1 -0
- package/dist/main/packages/SpeakLiveClient.d.ts +67 -0
- package/dist/main/packages/SpeakLiveClient.d.ts.map +1 -0
- package/dist/main/packages/SpeakLiveClient.js +148 -0
- package/dist/main/packages/SpeakLiveClient.js.map +1 -0
- package/dist/main/packages/SpeakRestClient.d.ts +0 -1
- package/dist/main/packages/SpeakRestClient.d.ts.map +1 -1
- package/dist/main/packages/SpeakRestClient.js +1 -2
- package/dist/main/packages/SpeakRestClient.js.map +1 -1
- package/dist/main/packages/index.d.ts +3 -0
- package/dist/main/packages/index.d.ts.map +1 -1
- package/dist/main/packages/index.js +3 -0
- package/dist/main/packages/index.js.map +1 -1
- package/dist/module/DeepgramClient.d.ts +7 -1
- package/dist/module/DeepgramClient.d.ts.map +1 -1
- package/dist/module/DeepgramClient.js +9 -1
- package/dist/module/DeepgramClient.js.map +1 -1
- package/dist/module/lib/enums/LiveTTSEvents.d.ts +34 -0
- package/dist/module/lib/enums/LiveTTSEvents.d.ts.map +1 -0
- package/dist/module/lib/enums/LiveTTSEvents.js +35 -0
- package/dist/module/lib/enums/LiveTTSEvents.js.map +1 -0
- package/dist/module/lib/enums/index.d.ts +1 -0
- package/dist/module/lib/enums/index.d.ts.map +1 -1
- package/dist/module/lib/enums/index.js +1 -0
- package/dist/module/lib/enums/index.js.map +1 -1
- package/dist/module/lib/types/GetModelsResponse.d.ts +24 -0
- package/dist/module/lib/types/GetModelsResponse.d.ts.map +1 -0
- package/dist/module/lib/types/GetModelsResponse.js +2 -0
- package/dist/module/lib/types/GetModelsResponse.js.map +1 -0
- package/dist/module/lib/types/GetModelsSchema.d.ts +4 -0
- package/dist/module/lib/types/GetModelsSchema.d.ts.map +1 -0
- package/dist/module/lib/types/GetModelsSchema.js +2 -0
- package/dist/module/lib/types/GetModelsSchema.js.map +1 -0
- package/dist/module/lib/types/index.d.ts +2 -0
- package/dist/module/lib/types/index.d.ts.map +1 -1
- package/dist/module/lib/types/index.js +2 -0
- package/dist/module/lib/types/index.js.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.js +1 -1
- package/dist/module/packages/ManageRestClient.d.ts +44 -1
- package/dist/module/packages/ManageRestClient.d.ts.map +1 -1
- package/dist/module/packages/ManageRestClient.js +71 -0
- package/dist/module/packages/ManageRestClient.js.map +1 -1
- package/dist/module/packages/ModelsRestClient.d.ts +53 -0
- package/dist/module/packages/ModelsRestClient.d.ts.map +1 -0
- package/dist/module/packages/ModelsRestClient.js +93 -0
- package/dist/module/packages/ModelsRestClient.js.map +1 -0
- package/dist/module/packages/SpeakClient.d.ts +27 -0
- package/dist/module/packages/SpeakClient.d.ts.map +1 -0
- package/dist/module/packages/SpeakClient.js +33 -0
- package/dist/module/packages/SpeakClient.js.map +1 -0
- package/dist/module/packages/SpeakLiveClient.d.ts +67 -0
- package/dist/module/packages/SpeakLiveClient.d.ts.map +1 -0
- package/dist/module/packages/SpeakLiveClient.js +144 -0
- package/dist/module/packages/SpeakLiveClient.js.map +1 -0
- package/dist/module/packages/SpeakRestClient.d.ts +0 -1
- package/dist/module/packages/SpeakRestClient.d.ts.map +1 -1
- package/dist/module/packages/SpeakRestClient.js +0 -1
- package/dist/module/packages/SpeakRestClient.js.map +1 -1
- package/dist/module/packages/index.d.ts +3 -0
- package/dist/module/packages/index.d.ts.map +1 -1
- package/dist/module/packages/index.js +3 -0
- package/dist/module/packages/index.js.map +1 -1
- package/dist/umd/deepgram.js +1 -1
- package/package.json +1 -1
- package/src/DeepgramClient.ts +10 -0
- package/src/lib/enums/LiveTTSEvents.ts +36 -0
- package/src/lib/enums/index.ts +1 -0
- package/src/lib/types/GetModelsResponse.ts +24 -0
- package/src/lib/types/GetModelsSchema.ts +3 -0
- package/src/lib/types/index.ts +2 -0
- package/src/lib/version.ts +1 -1
- package/src/packages/ManageRestClient.ts +82 -0
- package/src/packages/ModelsRestClient.ts +93 -0
- package/src/packages/SpeakClient.ts +35 -0
- package/src/packages/SpeakLiveClient.ts +163 -0
- package/src/packages/SpeakRestClient.ts +0 -2
- package/src/packages/index.ts +3 -0
package/src/DeepgramClient.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
OnPremClient,
|
|
8
8
|
SelfHostedRestClient,
|
|
9
9
|
SpeakClient,
|
|
10
|
+
ModelsRestClient,
|
|
10
11
|
} from "./packages";
|
|
11
12
|
|
|
12
13
|
/**
|
|
@@ -33,6 +34,15 @@ export default class DeepgramClient extends AbstractClient {
|
|
|
33
34
|
return new ManageClient(this.options);
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Returns a new instance of the ModelsRestClient, which provides access to the Deepgram API's model functionality.
|
|
39
|
+
*
|
|
40
|
+
* @returns {ModelsRestClient} A new instance of the ModelsRestClient.
|
|
41
|
+
*/
|
|
42
|
+
get models(): ModelsRestClient {
|
|
43
|
+
return new ModelsRestClient(this.options);
|
|
44
|
+
}
|
|
45
|
+
|
|
36
46
|
/**
|
|
37
47
|
* Returns a new instance of the SelfHostedRestClient, which provides access to the Deepgram API's self-hosted functionality.
|
|
38
48
|
*
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enumeration of events related to live text-to-speech synthesis.
|
|
3
|
+
*
|
|
4
|
+
* - `Open`: Built-in socket event for when the connection is opened.
|
|
5
|
+
* - `Close`: Built-in socket event for when the connection is closed.
|
|
6
|
+
* - `Error`: Built-in socket event for when an error occurs.
|
|
7
|
+
* - `Metadata`: Event for when metadata is received.
|
|
8
|
+
* - `Flushed`: Event for when the server has flushed the buffer.
|
|
9
|
+
* - `Warning`: Event for when a warning is received.
|
|
10
|
+
* - `Unhandled`: Catch-all event for any other message event.
|
|
11
|
+
*/
|
|
12
|
+
export enum LiveTTSEvents {
|
|
13
|
+
/**
|
|
14
|
+
* Built in socket events.
|
|
15
|
+
*/
|
|
16
|
+
Open = "Open",
|
|
17
|
+
Close = "Close",
|
|
18
|
+
Error = "Error",
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Message { type: string }
|
|
22
|
+
*/
|
|
23
|
+
Metadata = "Metadata",
|
|
24
|
+
Flushed = "Flushed",
|
|
25
|
+
Warning = "Warning",
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Audio data event.
|
|
29
|
+
*/
|
|
30
|
+
Audio = "Audio",
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Catch all for any other message event
|
|
34
|
+
*/
|
|
35
|
+
Unhandled = "Unhandled",
|
|
36
|
+
}
|
package/src/lib/enums/index.ts
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
type Model = {
|
|
2
|
+
name: string;
|
|
3
|
+
canonical_name: string;
|
|
4
|
+
architecture: string;
|
|
5
|
+
languages?: string[];
|
|
6
|
+
version: string;
|
|
7
|
+
uuid: string;
|
|
8
|
+
batch?: boolean;
|
|
9
|
+
streaming?: boolean;
|
|
10
|
+
formatted_output?: boolean;
|
|
11
|
+
metadata?: {
|
|
12
|
+
accent: string;
|
|
13
|
+
color: string;
|
|
14
|
+
image: string;
|
|
15
|
+
sample: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type GetModelResponse = Model;
|
|
20
|
+
|
|
21
|
+
export type GetModelsResponse = {
|
|
22
|
+
stt: Model[];
|
|
23
|
+
tts: Model[];
|
|
24
|
+
};
|
package/src/lib/types/index.ts
CHANGED
|
@@ -8,6 +8,8 @@ export * from "./DeepgramClientOptions";
|
|
|
8
8
|
export * from "./DeepgramResponse";
|
|
9
9
|
export * from "./DeepgramSource";
|
|
10
10
|
export * from "./Fetch";
|
|
11
|
+
export * from "./GetModelsResponse";
|
|
12
|
+
export * from "./GetModelsSchema";
|
|
11
13
|
export * from "./GetProjectBalancesResponse";
|
|
12
14
|
export * from "./GetProjectInvitesResponse";
|
|
13
15
|
export * from "./GetProjectKeysResponse";
|
package/src/lib/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "3.
|
|
1
|
+
export const version = "3.7.0";
|
|
@@ -25,6 +25,9 @@ import type {
|
|
|
25
25
|
UpdateProjectSchema,
|
|
26
26
|
VoidResponse,
|
|
27
27
|
GetTokenDetailsResponse,
|
|
28
|
+
GetModelsResponse,
|
|
29
|
+
GetModelResponse,
|
|
30
|
+
GetModelsSchema,
|
|
28
31
|
} from "../lib/types";
|
|
29
32
|
import { AbstractRestClient } from "./AbstractRestClient";
|
|
30
33
|
|
|
@@ -755,6 +758,85 @@ export class ManageRestClient extends AbstractRestClient {
|
|
|
755
758
|
throw error;
|
|
756
759
|
}
|
|
757
760
|
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* Retrieves all models for a given project.
|
|
764
|
+
*
|
|
765
|
+
* @param projectId - The ID of the project.
|
|
766
|
+
* @param endpoint - (optional) The endpoint URL for retrieving models. Defaults to ":version/projects/:projectId/models".
|
|
767
|
+
* @returns A promise that resolves to a DeepgramResponse containing the GetModelsResponse.
|
|
768
|
+
* @example
|
|
769
|
+
* ```typescript
|
|
770
|
+
* import { createClient } from "@deepgram/sdk";
|
|
771
|
+
*
|
|
772
|
+
* const deepgram = createClient(DEEPGRAM_API_KEY);
|
|
773
|
+
* const { result: models, error } = deepgram.manage.getAllModels("projectId");
|
|
774
|
+
*
|
|
775
|
+
* if (error) {
|
|
776
|
+
* console.error(error);
|
|
777
|
+
* } else {
|
|
778
|
+
* console.log(models);
|
|
779
|
+
* }
|
|
780
|
+
* ```
|
|
781
|
+
*/
|
|
782
|
+
async getAllModels(
|
|
783
|
+
projectId: string,
|
|
784
|
+
options: GetModelsSchema = {},
|
|
785
|
+
endpoint = ":version/projects/:projectId/models"
|
|
786
|
+
): Promise<DeepgramResponse<GetModelsResponse>> {
|
|
787
|
+
try {
|
|
788
|
+
const requestUrl = this.getRequestUrl(endpoint, { projectId }, options);
|
|
789
|
+
const result: GetModelsResponse = await this.get(requestUrl).then((result) => result.json());
|
|
790
|
+
|
|
791
|
+
return { result, error: null };
|
|
792
|
+
} catch (error) {
|
|
793
|
+
if (isDeepgramError(error)) {
|
|
794
|
+
return { result: null, error };
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
throw error;
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Retrieves a model from the specified project.
|
|
803
|
+
*
|
|
804
|
+
* @param projectId - The ID of the project.
|
|
805
|
+
* @param modelId - The ID of the model.
|
|
806
|
+
* @param endpoint - (optional) The endpoint URL for the request. Default value is ":version/projects/:projectId/models/:modelId".
|
|
807
|
+
* @returns A promise that resolves to a DeepgramResponse containing the GetModelResponse.
|
|
808
|
+
* @example
|
|
809
|
+
* ```typescript
|
|
810
|
+
* import { createClient } from "@deepgram/sdk";
|
|
811
|
+
*
|
|
812
|
+
* const deepgram = createClient(DEEPGRAM_API_KEY);
|
|
813
|
+
* const { result: model, error } = deepgram.models.getModel("projectId", "modelId");
|
|
814
|
+
*
|
|
815
|
+
* if (error) {
|
|
816
|
+
* console.error(error);
|
|
817
|
+
* } else {
|
|
818
|
+
* console.log(model);
|
|
819
|
+
* }
|
|
820
|
+
* ```
|
|
821
|
+
*/
|
|
822
|
+
async getModel(
|
|
823
|
+
projectId: string,
|
|
824
|
+
modelId: string,
|
|
825
|
+
endpoint = ":version/projects/:projectId/models/:modelId"
|
|
826
|
+
): Promise<DeepgramResponse<GetModelResponse>> {
|
|
827
|
+
try {
|
|
828
|
+
const requestUrl = this.getRequestUrl(endpoint, { projectId, modelId });
|
|
829
|
+
const result: GetModelResponse = await this.get(requestUrl).then((result) => result.json());
|
|
830
|
+
|
|
831
|
+
return { result, error: null };
|
|
832
|
+
} catch (error) {
|
|
833
|
+
if (isDeepgramError(error)) {
|
|
834
|
+
return { result: null, error };
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
throw error;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
758
840
|
}
|
|
759
841
|
|
|
760
842
|
export { ManageRestClient as ManageClient };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { isDeepgramError } from "../lib/errors";
|
|
2
|
+
import {
|
|
3
|
+
DeepgramResponse,
|
|
4
|
+
GetModelResponse,
|
|
5
|
+
GetModelsResponse,
|
|
6
|
+
GetModelsSchema,
|
|
7
|
+
} from "../lib/types";
|
|
8
|
+
import { AbstractRestClient } from "./AbstractRestClient";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Represents a REST client for interacting with the Deepgram API.
|
|
12
|
+
*
|
|
13
|
+
* The `ModelsRestClient` class provides methods for interacting with the Deepgram API to retrieve information about available models.
|
|
14
|
+
* @extends AbstractRestClient
|
|
15
|
+
*/
|
|
16
|
+
export class ModelsRestClient extends AbstractRestClient {
|
|
17
|
+
public namespace: string = "models";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Retrieves a list of all available models.
|
|
21
|
+
*
|
|
22
|
+
* @param endpoint - (optional) The endpoint to request.
|
|
23
|
+
* @returns A promise that resolves with the response from the Deepgram API.
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { createClient } from "@deepgram/sdk";
|
|
27
|
+
*
|
|
28
|
+
* const deepgram = createClient(DEEPGRAM_API_KEY);
|
|
29
|
+
* const { result: models, error } = deepgram.models.getAll();
|
|
30
|
+
*
|
|
31
|
+
* if (error) {
|
|
32
|
+
* console.error(error);
|
|
33
|
+
* } else {
|
|
34
|
+
* console.log(models);
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
async getAll(
|
|
39
|
+
endpoint = ":version/models",
|
|
40
|
+
options: GetModelsSchema = {}
|
|
41
|
+
): Promise<DeepgramResponse<GetModelsResponse>> {
|
|
42
|
+
try {
|
|
43
|
+
const requestUrl = this.getRequestUrl(endpoint, {}, options);
|
|
44
|
+
const result: GetModelsResponse = await this.get(requestUrl).then((result) => result.json());
|
|
45
|
+
|
|
46
|
+
return { result, error: null };
|
|
47
|
+
} catch (error) {
|
|
48
|
+
if (isDeepgramError(error)) {
|
|
49
|
+
return { result: null, error };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Retrieves information about a specific model.
|
|
58
|
+
*
|
|
59
|
+
* @param modelId - The UUID of the model to retrieve.
|
|
60
|
+
* @param endpoint - (optional) The endpoint to request.
|
|
61
|
+
* @returns A promise that resolves with the response from the Deepgram API.
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* import { createClient } from "@deepgram/sdk";
|
|
65
|
+
*
|
|
66
|
+
* const deepgram = createClient(DEEPGRAM_API_KEY);
|
|
67
|
+
* const { result: model, error } = deepgram.models.getModel("modelId");
|
|
68
|
+
*
|
|
69
|
+
* if (error) {
|
|
70
|
+
* console.error(error);
|
|
71
|
+
* } else {
|
|
72
|
+
* console.log(model);
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
async getModel(
|
|
77
|
+
modelId: string,
|
|
78
|
+
endpoint = ":version/models/:modelId"
|
|
79
|
+
): Promise<DeepgramResponse<GetModelResponse>> {
|
|
80
|
+
try {
|
|
81
|
+
const requestUrl = this.getRequestUrl(endpoint, { modelId });
|
|
82
|
+
const result: GetModelResponse = await this.get(requestUrl).then((result) => result.json());
|
|
83
|
+
|
|
84
|
+
return { result, error: null };
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (isDeepgramError(error)) {
|
|
87
|
+
return { result: null, error };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AbstractClient } from "./AbstractClient";
|
|
2
|
+
import { SpeakLiveClient } from "./SpeakLiveClient";
|
|
3
|
+
import { SpeakRestClient } from "./SpeakRestClient";
|
|
4
|
+
import { SpeakSchema } from "../lib/types";
|
|
5
|
+
import { TextSource } from "../lib/types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The `SpeakClient` class extends the `AbstractClient` class and provides access to the "speak" namespace.
|
|
9
|
+
* It exposes two methods:
|
|
10
|
+
*
|
|
11
|
+
* 1. `request()`: Returns a `SpeakRestClient` instance for interacting with the rest speak API.
|
|
12
|
+
* 2. `live(ttsOptions: SpeakSchema = {}, endpoint = ":version/speak")`: Returns a `SpeakLiveClient` instance for interacting with the live speak API, with the provided TTS options and endpoint.
|
|
13
|
+
*/
|
|
14
|
+
export class SpeakClient extends AbstractClient {
|
|
15
|
+
public namespace: string = "speak";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns a `SpeakRestClient` instance for interacting with the rest speak API.
|
|
19
|
+
*/
|
|
20
|
+
public request(source: TextSource, options?: SpeakSchema, endpoint = ":version/speak") {
|
|
21
|
+
const client = new SpeakRestClient(this.options);
|
|
22
|
+
|
|
23
|
+
return client.request(source, options, endpoint);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns a `SpeakLiveClient` instance for interacting with the live speak API, with the provided TTS options and endpoint.
|
|
28
|
+
* @param {SpeakSchema} [ttsOptions={}] - The TTS options to use for the live speak API.
|
|
29
|
+
* @param {string} [endpoint=":version/speak"] - The endpoint to use for the live speak API.
|
|
30
|
+
* @returns {SpeakLiveClient} - A `SpeakLiveClient` instance for interacting with the live speak API.
|
|
31
|
+
*/
|
|
32
|
+
public live(ttsOptions: SpeakSchema = {}, endpoint: string = ":version/speak"): SpeakLiveClient {
|
|
33
|
+
return new SpeakLiveClient(this.options, ttsOptions, endpoint);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { AbstractLiveClient } from "./AbstractLiveClient";
|
|
2
|
+
import { LiveTTSEvents } from "../lib/enums";
|
|
3
|
+
import type { SpeakSchema, DeepgramClientOptions } from "../lib/types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The `SpeakLiveClient` class extends the `AbstractLiveClient` class and provides functionality for setting up and managing a WebSocket connection for live text-to-speech synthesis.
|
|
7
|
+
*
|
|
8
|
+
* The constructor takes in `DeepgramClientOptions` and an optional `SpeakSchema` object, as well as an optional `endpoint` string. It then calls the `connect` method of the parent `AbstractLiveClient` class to establish the WebSocket connection.
|
|
9
|
+
*
|
|
10
|
+
* The `setupConnection` method is responsible for handling the various events that can occur on the WebSocket connection, such as opening, closing, and receiving messages. It sets up event handlers for these events and emits the appropriate events based on the message type.
|
|
11
|
+
*
|
|
12
|
+
* The `configure` method allows you to send additional configuration options to the connected session.
|
|
13
|
+
*
|
|
14
|
+
* The `requestClose` method requests the server to close the connection.
|
|
15
|
+
*/
|
|
16
|
+
export class SpeakLiveClient extends AbstractLiveClient {
|
|
17
|
+
public namespace: string = "speak";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Constructs a new `SpeakLiveClient` instance with the provided options.
|
|
21
|
+
*
|
|
22
|
+
* @param options - The `DeepgramClientOptions` to use for the client connection.
|
|
23
|
+
* @param speakOptions - An optional `SpeakSchema` object containing additional configuration options for the text-to-speech.
|
|
24
|
+
* @param endpoint - An optional string representing the WebSocket endpoint to connect to. Defaults to `:version/speak`.
|
|
25
|
+
*/
|
|
26
|
+
constructor(
|
|
27
|
+
options: DeepgramClientOptions,
|
|
28
|
+
speakOptions: Omit<SpeakSchema, "container"> = {},
|
|
29
|
+
endpoint: string = ":version/speak"
|
|
30
|
+
) {
|
|
31
|
+
super(options);
|
|
32
|
+
|
|
33
|
+
this.connect(speakOptions, endpoint);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sets up the connection event handlers.
|
|
38
|
+
* This method is responsible for handling the various events that can occur on the WebSocket connection, such as opening, closing, and receiving data.
|
|
39
|
+
* - When the connection is opened, it emits the `LiveTTSEvents.Open` event.
|
|
40
|
+
* - When the connection is closed, it emits the `LiveTTSEvents.Close` event.
|
|
41
|
+
* - When an error occurs on the connection, it emits the `LiveTTSEvents.Error` event.
|
|
42
|
+
* - When a message is received, it parses the message and emits the appropriate event based on the message type, such as `LiveTTSEvents.Metadata`, `LiveTTSEvents.Flushed`, and `LiveTTSEvents.Warning`.
|
|
43
|
+
*/
|
|
44
|
+
public setupConnection(): void {
|
|
45
|
+
if (this.conn) {
|
|
46
|
+
this.conn.onopen = () => {
|
|
47
|
+
this.emit(LiveTTSEvents.Open, this);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
this.conn.onclose = (event: any) => {
|
|
51
|
+
this.emit(LiveTTSEvents.Close, event);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
this.conn.onerror = (event: ErrorEvent) => {
|
|
55
|
+
this.emit(LiveTTSEvents.Error, event);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
this.conn.onmessage = (event: MessageEvent) => {
|
|
59
|
+
this.handleMessage(event);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Handles text messages received from the WebSocket connection.
|
|
66
|
+
* @param data - The parsed JSON data.
|
|
67
|
+
*/
|
|
68
|
+
protected handleTextMessage(data: any): void {
|
|
69
|
+
if (data.type === LiveTTSEvents.Metadata) {
|
|
70
|
+
this.emit(LiveTTSEvents.Metadata, data);
|
|
71
|
+
} else if (data.type === LiveTTSEvents.Flushed) {
|
|
72
|
+
this.emit(LiveTTSEvents.Flushed, data);
|
|
73
|
+
} else if (data.type === LiveTTSEvents.Warning) {
|
|
74
|
+
this.emit(LiveTTSEvents.Warning, data);
|
|
75
|
+
} else {
|
|
76
|
+
this.emit(LiveTTSEvents.Unhandled, data);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Handles binary messages received from the WebSocket connection.
|
|
82
|
+
* @param data - The binary data.
|
|
83
|
+
*/
|
|
84
|
+
protected handleBinaryMessage(data: ArrayBuffer): void {
|
|
85
|
+
this.emit(LiveTTSEvents.Audio, data);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Sends a text input message to the server.
|
|
90
|
+
*
|
|
91
|
+
* @param {string} text - The text to convert to speech.
|
|
92
|
+
*/
|
|
93
|
+
public sendText(text: string): void {
|
|
94
|
+
this.send(
|
|
95
|
+
JSON.stringify({
|
|
96
|
+
type: "Speak",
|
|
97
|
+
text,
|
|
98
|
+
})
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Requests the server flush the current buffer and return generated audio.
|
|
104
|
+
*/
|
|
105
|
+
public flush(): void {
|
|
106
|
+
this.send(
|
|
107
|
+
JSON.stringify({
|
|
108
|
+
type: "Flush",
|
|
109
|
+
})
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Requests the server clear the current buffer.
|
|
115
|
+
*/
|
|
116
|
+
public clear(): void {
|
|
117
|
+
this.send(
|
|
118
|
+
JSON.stringify({
|
|
119
|
+
type: "Clear",
|
|
120
|
+
})
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Requests the server close the connection.
|
|
126
|
+
*/
|
|
127
|
+
public requestClose(): void {
|
|
128
|
+
this.send(
|
|
129
|
+
JSON.stringify({
|
|
130
|
+
type: "Close",
|
|
131
|
+
})
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Handles incoming messages from the WebSocket connection.
|
|
137
|
+
* @param event - The MessageEvent object representing the received message.
|
|
138
|
+
*/
|
|
139
|
+
protected handleMessage(event: MessageEvent): void {
|
|
140
|
+
if (typeof event.data === "string") {
|
|
141
|
+
try {
|
|
142
|
+
const data = JSON.parse(event.data);
|
|
143
|
+
this.handleTextMessage(data);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
this.emit(LiveTTSEvents.Error, {
|
|
146
|
+
event,
|
|
147
|
+
message: "Unable to parse `data` as JSON.",
|
|
148
|
+
error,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
} else if (event.data instanceof ArrayBuffer) {
|
|
152
|
+
this.handleBinaryMessage(event.data);
|
|
153
|
+
} else if (Buffer.isBuffer(event.data)) {
|
|
154
|
+
this.handleBinaryMessage(event.data.buffer);
|
|
155
|
+
} else {
|
|
156
|
+
console.log("Received unknown data type", event.data);
|
|
157
|
+
this.emit(LiveTTSEvents.Error, {
|
|
158
|
+
event,
|
|
159
|
+
message: "Received unknown data type.",
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
package/src/packages/index.ts
CHANGED
|
@@ -5,6 +5,9 @@ export * from "./ListenClient";
|
|
|
5
5
|
export * from "./ListenLiveClient";
|
|
6
6
|
export * from "./ListenRestClient";
|
|
7
7
|
export * from "./ManageRestClient";
|
|
8
|
+
export * from "./ModelsRestClient";
|
|
8
9
|
export * from "./ReadRestClient";
|
|
9
10
|
export * from "./SelfHostedRestClient";
|
|
11
|
+
export * from "./SpeakClient";
|
|
12
|
+
export * from "./SpeakLiveClient";
|
|
10
13
|
export * from "./SpeakRestClient";
|