-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nova.js
192 lines (170 loc) · 4.99 KB
/
Nova.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
145
146
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
/** This is the main class for interacting with the Nova API.
* You should only ever instantiate one and reuse it throughout your app.
* Multiple attempts to instantiate this will give the same object (it is a singleton). */
class NovaClient {
static #_instance = null;
#teamId = "";
#serverURL = "";
#apiKeys = {};
/** @returns {string} - The server URL used by this client. */
get serverURL() {
return this.#serverURL;
}
/** @param {string} teamId - The team ID you want to interact with.
* @param {string} serverURL - The server to work with.
* @returns {NovaClient} - The singleton instance of this class.
* If already instantiated, returns the same instance, discarding any new configurations.
*/
constructor(teamId= "", serverURL = "") {
if (!this.#_instance) {
this.#_instance = this;
this.#teamId = teamId;
this.#serverURL = serverURL;
this.#loadAPIKeysFromServer();
}
return this.#_instance;
}
#loadAPIKeysFromServer() {
const url = this.#serverURL + "/keys";
const params = {'teamId': this.#teamId};
try {
let response = fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(params)
});
if (!response.ok) {
throw new Error("Failed to fetch API keys: " + response.statusText);
}
this.#apiKeys = response.json();
} catch (error) {
console.error("Error fetching API keys: " + error);
throw error;
}
}
/**
* @readonly
* @enum {string}
*/
Modality = {
TEXT: "text",
IMAGE: "image",
AUDIO: "audio",
}
/**
* @param {Message} message
* @param {Modality} outputModality
*/
processMessage(message, outputModality) {
const url = this.#serverURL + "/process_message";
let data = {
'text': message.text,
'modality': outputModality,
}
let files = [];
if (message.hasImages()) {
for (let image_path of message.images) {
files.push({'image': fetch(image_path)});
}
}
if (message.hasAudio()) {
for (let audio_path of message.audio) {
files.push({'audio': fetch(audio_path)});
}
}
data.files = files;
let response = fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error("Failed to process message: " + response.statusText);
}
if (outputModality === this.Modality.TEXT) {
return response.text();
} else {
return response.blob();
}
}
}
/**
* Represents a message containing text, images, and audio.
*/
class Message {
text = "";
/** An array of URLs pointing to images.
* @type {string[]} */
images = [];
/** An array of URLs pointing to audio files.
* @type {string[]} */
audio = [];
/**
* Constructs a new Message object.
* @param {string} text The text to include in the message
* @param {string[]} images The URLs of images to include in the message
* @param {string[]} audio The URLs of audio files to include in the message
*/
constructor(text = "", images = [], audio = []) {
this.text = text;
this.images = images;
this.audio = audio;
}
hasText() {
return this.text.length > 0;
}
hasImages() {
return this.images.length > 0;
}
hasAudio() {
return this.audio.length > 0;
}
}
/**
* The TextToSpeech class provides text-to-speech functionality.
*/
class TextToSpeech {
/**
* @readonly
* @enum {string}
*/
Provider = {
CARTESIA: "cartesia",
HUME: "hume",
}
/** @type {Provider} */
provider = this.Provider.CARTESIA;
/**
* Constructs a new TextToSpeech synthesizer, using the given TTS provider.
* @param {Provider} provider
*/
constructor(provider = this.Provider.CARTESIA) {
this.provider = provider;
}
/**
* Synthesizes the given text into audio.
* @param {string} text
*/
synthesize(text) {
const url = (new NovaClient()).serverURL + "/synthesize";
let data = {
'text': text,
'provider': this.provider,
}
let response = fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error("Failed to synthesize text: " + response.statusText);
}
return response.blob();
}
}