assemblyai 4.12.2 → 4.13.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/assemblyai.umd.js +212 -4
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +209 -5
- package/dist/bun.mjs +209 -5
- package/dist/deno.mjs +209 -5
- package/dist/index.cjs +212 -4
- package/dist/index.mjs +212 -5
- package/dist/node.cjs +209 -4
- package/dist/node.mjs +209 -5
- package/dist/services/index.d.ts +6 -1
- package/dist/services/streaming/factory.d.ts +11 -0
- package/dist/services/streaming/index.d.ts +2 -0
- package/dist/services/streaming/service.d.ts +20 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/services/index.d.ts +1 -0
- package/dist/types/streaming/index.d.ts +69 -0
- package/dist/utils/errors/index.d.ts +1 -0
- package/dist/utils/errors/streaming.d.ts +23 -0
- package/dist/workerd.mjs +209 -5
- package/package.json +1 -1
- package/src/services/index.ts +14 -0
- package/src/services/streaming/factory.ts +48 -0
- package/src/services/streaming/index.ts +2 -0
- package/src/services/streaming/service.ts +209 -0
- package/src/types/index.ts +2 -1
- package/src/types/services/index.ts +1 -0
- package/src/types/streaming/index.ts +92 -0
- package/src/utils/errors/index.ts +6 -0
- package/src/utils/errors/streaming.ts +51 -0
package/dist/bun.mjs
CHANGED
|
@@ -17,7 +17,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
17
17
|
defaultUserAgentString += navigator.userAgent;
|
|
18
18
|
}
|
|
19
19
|
const defaultUserAgent = {
|
|
20
|
-
sdk: { name: "JavaScript", version: "4.
|
|
20
|
+
sdk: { name: "JavaScript", version: "4.13.0" },
|
|
21
21
|
};
|
|
22
22
|
if (typeof process !== "undefined") {
|
|
23
23
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
@@ -199,9 +199,48 @@ const RealtimeErrorMessages = {
|
|
|
199
199
|
class RealtimeError extends Error {
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
+
const StreamingErrorType = {
|
|
203
|
+
BadSampleRate: 4000,
|
|
204
|
+
AuthFailed: 4001,
|
|
205
|
+
InsufficientFunds: 4002,
|
|
206
|
+
FreeTierUser: 4003,
|
|
207
|
+
NonexistentSessionId: 4004,
|
|
208
|
+
SessionExpired: 4008,
|
|
209
|
+
ClosedSession: 4010,
|
|
210
|
+
RateLimited: 4029,
|
|
211
|
+
UniqueSessionViolation: 4030,
|
|
212
|
+
SessionTimeout: 4031,
|
|
213
|
+
AudioTooShort: 4032,
|
|
214
|
+
AudioTooLong: 4033,
|
|
215
|
+
AudioTooSmallToTranscode: 4034,
|
|
216
|
+
BadSchema: 4101,
|
|
217
|
+
TooManyStreams: 4102,
|
|
218
|
+
Reconnected: 4103,
|
|
219
|
+
};
|
|
220
|
+
const StreamingErrorMessages = {
|
|
221
|
+
[StreamingErrorType.BadSampleRate]: "Sample rate must be a positive integer",
|
|
222
|
+
[StreamingErrorType.AuthFailed]: "Not Authorized",
|
|
223
|
+
[StreamingErrorType.InsufficientFunds]: "Insufficient funds",
|
|
224
|
+
[StreamingErrorType.FreeTierUser]: "This feature is paid-only and requires you to add a credit card. Please visit https://app.assemblyai.com/ to add a credit card to your account.",
|
|
225
|
+
[StreamingErrorType.NonexistentSessionId]: "Session ID does not exist",
|
|
226
|
+
[StreamingErrorType.SessionExpired]: "Session has expired",
|
|
227
|
+
[StreamingErrorType.ClosedSession]: "Session is closed",
|
|
228
|
+
[StreamingErrorType.RateLimited]: "Rate limited",
|
|
229
|
+
[StreamingErrorType.UniqueSessionViolation]: "Unique session violation",
|
|
230
|
+
[StreamingErrorType.SessionTimeout]: "Session Timeout",
|
|
231
|
+
[StreamingErrorType.AudioTooShort]: "Audio too short",
|
|
232
|
+
[StreamingErrorType.AudioTooLong]: "Audio too long",
|
|
233
|
+
[StreamingErrorType.AudioTooSmallToTranscode]: "Audio too small to transcode",
|
|
234
|
+
[StreamingErrorType.BadSchema]: "Bad schema",
|
|
235
|
+
[StreamingErrorType.TooManyStreams]: "Too many streams",
|
|
236
|
+
[StreamingErrorType.Reconnected]: "This session has been reconnected. This WebSocket is no longer valid.",
|
|
237
|
+
};
|
|
238
|
+
class StreamingError extends Error {
|
|
239
|
+
}
|
|
240
|
+
|
|
202
241
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
203
242
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
204
|
-
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
243
|
+
const terminateSessionMessage$1 = `{"terminate_session":true}`;
|
|
205
244
|
/**
|
|
206
245
|
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
207
246
|
*/
|
|
@@ -390,11 +429,11 @@ class RealtimeTranscriber {
|
|
|
390
429
|
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
391
430
|
this.sessionTerminatedResolve = resolve;
|
|
392
431
|
});
|
|
393
|
-
this.socket.send(terminateSessionMessage);
|
|
432
|
+
this.socket.send(terminateSessionMessage$1);
|
|
394
433
|
await sessionTerminatedPromise;
|
|
395
434
|
}
|
|
396
435
|
else {
|
|
397
|
-
this.socket.send(terminateSessionMessage);
|
|
436
|
+
this.socket.send(terminateSessionMessage$1);
|
|
398
437
|
}
|
|
399
438
|
}
|
|
400
439
|
if (this.socket?.removeAllListeners)
|
|
@@ -727,7 +766,168 @@ function dataUrlToBlob(dataUrl) {
|
|
|
727
766
|
return new Blob([u8arr], { type: mime });
|
|
728
767
|
}
|
|
729
768
|
|
|
769
|
+
const defaultStreamingUrl$1 = "wss://streaming.assemblyai.com/v3/ws";
|
|
770
|
+
const terminateSessionMessage = `{"type":"Terminate"}`;
|
|
771
|
+
class StreamingTranscriber {
|
|
772
|
+
constructor(params) {
|
|
773
|
+
this.listeners = {};
|
|
774
|
+
this.params = {
|
|
775
|
+
...params,
|
|
776
|
+
websocketBaseUrl: params.websocketBaseUrl || defaultStreamingUrl$1,
|
|
777
|
+
};
|
|
778
|
+
if ("token" in params && params.token)
|
|
779
|
+
this.token = params.token;
|
|
780
|
+
if ("apiKey" in params && params.apiKey)
|
|
781
|
+
this.apiKey = params.apiKey;
|
|
782
|
+
if (!(this.token || this.apiKey)) {
|
|
783
|
+
throw new Error("API key or temporary token is required.");
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
connectionUrl() {
|
|
787
|
+
const url = new URL(this.params.websocketBaseUrl ?? "");
|
|
788
|
+
if (url.protocol !== "wss:") {
|
|
789
|
+
throw new Error("Invalid protocol, must be wss");
|
|
790
|
+
}
|
|
791
|
+
const searchParams = new URLSearchParams();
|
|
792
|
+
searchParams.set("sample_rate", this.params.sampleRate.toString());
|
|
793
|
+
if (this.params.endOfTurnConfidenceThreshold) {
|
|
794
|
+
searchParams.set("end_of_turn_confidence_threshold", this.params.endOfTurnConfidenceThreshold.toString());
|
|
795
|
+
}
|
|
796
|
+
if (this.params.minEndOfTurnSilenceWhenConfident) {
|
|
797
|
+
searchParams.set("min_end_of_turn_silence_when_confident", this.params.minEndOfTurnSilenceWhenConfident.toString());
|
|
798
|
+
}
|
|
799
|
+
if (this.params.maxTurnSilence) {
|
|
800
|
+
searchParams.set("max_turn_silence", this.params.maxTurnSilence.toString());
|
|
801
|
+
}
|
|
802
|
+
if (this.params.formatTurns) {
|
|
803
|
+
searchParams.set("format_turns", this.params.formatTurns.toString());
|
|
804
|
+
}
|
|
805
|
+
url.search = searchParams.toString();
|
|
806
|
+
return url;
|
|
807
|
+
}
|
|
808
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
809
|
+
on(event, listener) {
|
|
810
|
+
this.listeners[event] = listener;
|
|
811
|
+
}
|
|
812
|
+
connect() {
|
|
813
|
+
return new Promise((resolve) => {
|
|
814
|
+
if (this.socket) {
|
|
815
|
+
throw new Error("Already connected");
|
|
816
|
+
}
|
|
817
|
+
const url = this.connectionUrl();
|
|
818
|
+
this.socket = factory(url.toString(), {
|
|
819
|
+
headers: { Authorization: this.token || this.apiKey },
|
|
820
|
+
});
|
|
821
|
+
this.socket.binaryType = "arraybuffer";
|
|
822
|
+
this.socket.onopen = () => { };
|
|
823
|
+
this.socket.onclose = ({ code, reason }) => {
|
|
824
|
+
if (!reason) {
|
|
825
|
+
if (code in StreamingErrorMessages) {
|
|
826
|
+
reason = StreamingErrorMessages[code];
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
this.listeners.close?.(code, reason);
|
|
830
|
+
};
|
|
831
|
+
this.socket.onerror = (event) => {
|
|
832
|
+
if (event.error)
|
|
833
|
+
this.listeners.error?.(event.error);
|
|
834
|
+
else
|
|
835
|
+
this.listeners.error?.(new Error(event.message));
|
|
836
|
+
};
|
|
837
|
+
this.socket.onmessage = ({ data }) => {
|
|
838
|
+
const message = JSON.parse(data.toString());
|
|
839
|
+
if ("error" in message) {
|
|
840
|
+
this.listeners.error?.(new StreamingError(message.error));
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
switch (message.type) {
|
|
844
|
+
case "Begin": {
|
|
845
|
+
resolve(message);
|
|
846
|
+
this.listeners.open?.(message);
|
|
847
|
+
break;
|
|
848
|
+
}
|
|
849
|
+
case "Turn": {
|
|
850
|
+
this.listeners.turn?.(message);
|
|
851
|
+
break;
|
|
852
|
+
}
|
|
853
|
+
case "Termination": {
|
|
854
|
+
this.sessionTerminatedResolve?.();
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
stream() {
|
|
862
|
+
return new WritableStream({
|
|
863
|
+
write: (chunk) => {
|
|
864
|
+
this.sendAudio(chunk);
|
|
865
|
+
},
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
sendAudio(audio) {
|
|
869
|
+
this.send(audio);
|
|
870
|
+
}
|
|
871
|
+
send(data) {
|
|
872
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
873
|
+
throw new Error("Socket is not open for communication");
|
|
874
|
+
}
|
|
875
|
+
this.socket.send(data);
|
|
876
|
+
}
|
|
877
|
+
async close(waitForSessionTermination = true) {
|
|
878
|
+
if (this.socket) {
|
|
879
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
880
|
+
if (waitForSessionTermination) {
|
|
881
|
+
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
882
|
+
this.sessionTerminatedResolve = resolve;
|
|
883
|
+
});
|
|
884
|
+
this.socket.send(terminateSessionMessage);
|
|
885
|
+
await sessionTerminatedPromise;
|
|
886
|
+
}
|
|
887
|
+
else {
|
|
888
|
+
this.socket.send(terminateSessionMessage);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
if (this.socket?.removeAllListeners)
|
|
892
|
+
this.socket.removeAllListeners();
|
|
893
|
+
this.socket.close();
|
|
894
|
+
}
|
|
895
|
+
this.listeners = {};
|
|
896
|
+
this.socket = undefined;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
class StreamingTranscriberFactory extends BaseService {
|
|
901
|
+
constructor(params) {
|
|
902
|
+
super(params);
|
|
903
|
+
this.baseServiceParams = params;
|
|
904
|
+
}
|
|
905
|
+
transcriber(params) {
|
|
906
|
+
const serviceParams = { ...params };
|
|
907
|
+
if (!serviceParams.token && !serviceParams.apiKey) {
|
|
908
|
+
serviceParams.apiKey = this.baseServiceParams.apiKey;
|
|
909
|
+
}
|
|
910
|
+
return new StreamingTranscriber(serviceParams);
|
|
911
|
+
}
|
|
912
|
+
async createTemporaryToken(params) {
|
|
913
|
+
const searchParams = new URLSearchParams();
|
|
914
|
+
// Add each param to the search params
|
|
915
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
916
|
+
if (value !== undefined && value !== null) {
|
|
917
|
+
searchParams.append(key, String(value));
|
|
918
|
+
}
|
|
919
|
+
});
|
|
920
|
+
const queryString = searchParams.toString();
|
|
921
|
+
const url = queryString ? `/v3/token?${queryString}` : "/v3/token";
|
|
922
|
+
const data = await this.fetchJson(url, {
|
|
923
|
+
method: "GET",
|
|
924
|
+
});
|
|
925
|
+
return data.token;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
|
|
730
929
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
930
|
+
const defaultStreamingUrl = "https://streaming.assemblyai.com";
|
|
731
931
|
class AssemblyAI {
|
|
732
932
|
/**
|
|
733
933
|
* Create a new AssemblyAI client.
|
|
@@ -742,7 +942,11 @@ class AssemblyAI {
|
|
|
742
942
|
this.transcripts = new TranscriptService(params, this.files);
|
|
743
943
|
this.lemur = new LemurService(params);
|
|
744
944
|
this.realtime = new RealtimeTranscriberFactory(params);
|
|
945
|
+
this.streaming = new StreamingTranscriberFactory({
|
|
946
|
+
...params,
|
|
947
|
+
baseUrl: params.streamingBaseUrl || defaultStreamingUrl,
|
|
948
|
+
});
|
|
745
949
|
}
|
|
746
950
|
}
|
|
747
951
|
|
|
748
|
-
export { AssemblyAI, FileService, LemurService, RealtimeService, RealtimeServiceFactory, RealtimeTranscriber, RealtimeTranscriberFactory, TranscriptService };
|
|
952
|
+
export { AssemblyAI, FileService, LemurService, RealtimeService, RealtimeServiceFactory, RealtimeTranscriber, RealtimeTranscriberFactory, StreamingTranscriber, TranscriptService };
|
package/dist/deno.mjs
CHANGED
|
@@ -17,7 +17,7 @@ if (typeof navigator !== "undefined" && navigator.userAgent) {
|
|
|
17
17
|
defaultUserAgentString += navigator.userAgent;
|
|
18
18
|
}
|
|
19
19
|
const defaultUserAgent = {
|
|
20
|
-
sdk: { name: "JavaScript", version: "4.
|
|
20
|
+
sdk: { name: "JavaScript", version: "4.13.0" },
|
|
21
21
|
};
|
|
22
22
|
if (typeof process !== "undefined") {
|
|
23
23
|
if (process.versions.node && defaultUserAgentString.indexOf("Node") === -1) {
|
|
@@ -199,9 +199,48 @@ const RealtimeErrorMessages = {
|
|
|
199
199
|
class RealtimeError extends Error {
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
+
const StreamingErrorType = {
|
|
203
|
+
BadSampleRate: 4000,
|
|
204
|
+
AuthFailed: 4001,
|
|
205
|
+
InsufficientFunds: 4002,
|
|
206
|
+
FreeTierUser: 4003,
|
|
207
|
+
NonexistentSessionId: 4004,
|
|
208
|
+
SessionExpired: 4008,
|
|
209
|
+
ClosedSession: 4010,
|
|
210
|
+
RateLimited: 4029,
|
|
211
|
+
UniqueSessionViolation: 4030,
|
|
212
|
+
SessionTimeout: 4031,
|
|
213
|
+
AudioTooShort: 4032,
|
|
214
|
+
AudioTooLong: 4033,
|
|
215
|
+
AudioTooSmallToTranscode: 4034,
|
|
216
|
+
BadSchema: 4101,
|
|
217
|
+
TooManyStreams: 4102,
|
|
218
|
+
Reconnected: 4103,
|
|
219
|
+
};
|
|
220
|
+
const StreamingErrorMessages = {
|
|
221
|
+
[StreamingErrorType.BadSampleRate]: "Sample rate must be a positive integer",
|
|
222
|
+
[StreamingErrorType.AuthFailed]: "Not Authorized",
|
|
223
|
+
[StreamingErrorType.InsufficientFunds]: "Insufficient funds",
|
|
224
|
+
[StreamingErrorType.FreeTierUser]: "This feature is paid-only and requires you to add a credit card. Please visit https://app.assemblyai.com/ to add a credit card to your account.",
|
|
225
|
+
[StreamingErrorType.NonexistentSessionId]: "Session ID does not exist",
|
|
226
|
+
[StreamingErrorType.SessionExpired]: "Session has expired",
|
|
227
|
+
[StreamingErrorType.ClosedSession]: "Session is closed",
|
|
228
|
+
[StreamingErrorType.RateLimited]: "Rate limited",
|
|
229
|
+
[StreamingErrorType.UniqueSessionViolation]: "Unique session violation",
|
|
230
|
+
[StreamingErrorType.SessionTimeout]: "Session Timeout",
|
|
231
|
+
[StreamingErrorType.AudioTooShort]: "Audio too short",
|
|
232
|
+
[StreamingErrorType.AudioTooLong]: "Audio too long",
|
|
233
|
+
[StreamingErrorType.AudioTooSmallToTranscode]: "Audio too small to transcode",
|
|
234
|
+
[StreamingErrorType.BadSchema]: "Bad schema",
|
|
235
|
+
[StreamingErrorType.TooManyStreams]: "Too many streams",
|
|
236
|
+
[StreamingErrorType.Reconnected]: "This session has been reconnected. This WebSocket is no longer valid.",
|
|
237
|
+
};
|
|
238
|
+
class StreamingError extends Error {
|
|
239
|
+
}
|
|
240
|
+
|
|
202
241
|
const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
|
|
203
242
|
const forceEndOfUtteranceMessage = `{"force_end_utterance":true}`;
|
|
204
|
-
const terminateSessionMessage = `{"terminate_session":true}`;
|
|
243
|
+
const terminateSessionMessage$1 = `{"terminate_session":true}`;
|
|
205
244
|
/**
|
|
206
245
|
* RealtimeTranscriber connects to the Streaming Speech-to-Text API and lets you transcribe audio in real-time.
|
|
207
246
|
*/
|
|
@@ -390,11 +429,11 @@ class RealtimeTranscriber {
|
|
|
390
429
|
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
391
430
|
this.sessionTerminatedResolve = resolve;
|
|
392
431
|
});
|
|
393
|
-
this.socket.send(terminateSessionMessage);
|
|
432
|
+
this.socket.send(terminateSessionMessage$1);
|
|
394
433
|
await sessionTerminatedPromise;
|
|
395
434
|
}
|
|
396
435
|
else {
|
|
397
|
-
this.socket.send(terminateSessionMessage);
|
|
436
|
+
this.socket.send(terminateSessionMessage$1);
|
|
398
437
|
}
|
|
399
438
|
}
|
|
400
439
|
if (this.socket?.removeAllListeners)
|
|
@@ -727,7 +766,168 @@ function dataUrlToBlob(dataUrl) {
|
|
|
727
766
|
return new Blob([u8arr], { type: mime });
|
|
728
767
|
}
|
|
729
768
|
|
|
769
|
+
const defaultStreamingUrl$1 = "wss://streaming.assemblyai.com/v3/ws";
|
|
770
|
+
const terminateSessionMessage = `{"type":"Terminate"}`;
|
|
771
|
+
class StreamingTranscriber {
|
|
772
|
+
constructor(params) {
|
|
773
|
+
this.listeners = {};
|
|
774
|
+
this.params = {
|
|
775
|
+
...params,
|
|
776
|
+
websocketBaseUrl: params.websocketBaseUrl || defaultStreamingUrl$1,
|
|
777
|
+
};
|
|
778
|
+
if ("token" in params && params.token)
|
|
779
|
+
this.token = params.token;
|
|
780
|
+
if ("apiKey" in params && params.apiKey)
|
|
781
|
+
this.apiKey = params.apiKey;
|
|
782
|
+
if (!(this.token || this.apiKey)) {
|
|
783
|
+
throw new Error("API key or temporary token is required.");
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
connectionUrl() {
|
|
787
|
+
const url = new URL(this.params.websocketBaseUrl ?? "");
|
|
788
|
+
if (url.protocol !== "wss:") {
|
|
789
|
+
throw new Error("Invalid protocol, must be wss");
|
|
790
|
+
}
|
|
791
|
+
const searchParams = new URLSearchParams();
|
|
792
|
+
searchParams.set("sample_rate", this.params.sampleRate.toString());
|
|
793
|
+
if (this.params.endOfTurnConfidenceThreshold) {
|
|
794
|
+
searchParams.set("end_of_turn_confidence_threshold", this.params.endOfTurnConfidenceThreshold.toString());
|
|
795
|
+
}
|
|
796
|
+
if (this.params.minEndOfTurnSilenceWhenConfident) {
|
|
797
|
+
searchParams.set("min_end_of_turn_silence_when_confident", this.params.minEndOfTurnSilenceWhenConfident.toString());
|
|
798
|
+
}
|
|
799
|
+
if (this.params.maxTurnSilence) {
|
|
800
|
+
searchParams.set("max_turn_silence", this.params.maxTurnSilence.toString());
|
|
801
|
+
}
|
|
802
|
+
if (this.params.formatTurns) {
|
|
803
|
+
searchParams.set("format_turns", this.params.formatTurns.toString());
|
|
804
|
+
}
|
|
805
|
+
url.search = searchParams.toString();
|
|
806
|
+
return url;
|
|
807
|
+
}
|
|
808
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
809
|
+
on(event, listener) {
|
|
810
|
+
this.listeners[event] = listener;
|
|
811
|
+
}
|
|
812
|
+
connect() {
|
|
813
|
+
return new Promise((resolve) => {
|
|
814
|
+
if (this.socket) {
|
|
815
|
+
throw new Error("Already connected");
|
|
816
|
+
}
|
|
817
|
+
const url = this.connectionUrl();
|
|
818
|
+
this.socket = factory(url.toString(), {
|
|
819
|
+
headers: { Authorization: this.token || this.apiKey },
|
|
820
|
+
});
|
|
821
|
+
this.socket.binaryType = "arraybuffer";
|
|
822
|
+
this.socket.onopen = () => { };
|
|
823
|
+
this.socket.onclose = ({ code, reason }) => {
|
|
824
|
+
if (!reason) {
|
|
825
|
+
if (code in StreamingErrorMessages) {
|
|
826
|
+
reason = StreamingErrorMessages[code];
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
this.listeners.close?.(code, reason);
|
|
830
|
+
};
|
|
831
|
+
this.socket.onerror = (event) => {
|
|
832
|
+
if (event.error)
|
|
833
|
+
this.listeners.error?.(event.error);
|
|
834
|
+
else
|
|
835
|
+
this.listeners.error?.(new Error(event.message));
|
|
836
|
+
};
|
|
837
|
+
this.socket.onmessage = ({ data }) => {
|
|
838
|
+
const message = JSON.parse(data.toString());
|
|
839
|
+
if ("error" in message) {
|
|
840
|
+
this.listeners.error?.(new StreamingError(message.error));
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
switch (message.type) {
|
|
844
|
+
case "Begin": {
|
|
845
|
+
resolve(message);
|
|
846
|
+
this.listeners.open?.(message);
|
|
847
|
+
break;
|
|
848
|
+
}
|
|
849
|
+
case "Turn": {
|
|
850
|
+
this.listeners.turn?.(message);
|
|
851
|
+
break;
|
|
852
|
+
}
|
|
853
|
+
case "Termination": {
|
|
854
|
+
this.sessionTerminatedResolve?.();
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
stream() {
|
|
862
|
+
return new WritableStream({
|
|
863
|
+
write: (chunk) => {
|
|
864
|
+
this.sendAudio(chunk);
|
|
865
|
+
},
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
sendAudio(audio) {
|
|
869
|
+
this.send(audio);
|
|
870
|
+
}
|
|
871
|
+
send(data) {
|
|
872
|
+
if (!this.socket || this.socket.readyState !== this.socket.OPEN) {
|
|
873
|
+
throw new Error("Socket is not open for communication");
|
|
874
|
+
}
|
|
875
|
+
this.socket.send(data);
|
|
876
|
+
}
|
|
877
|
+
async close(waitForSessionTermination = true) {
|
|
878
|
+
if (this.socket) {
|
|
879
|
+
if (this.socket.readyState === this.socket.OPEN) {
|
|
880
|
+
if (waitForSessionTermination) {
|
|
881
|
+
const sessionTerminatedPromise = new Promise((resolve) => {
|
|
882
|
+
this.sessionTerminatedResolve = resolve;
|
|
883
|
+
});
|
|
884
|
+
this.socket.send(terminateSessionMessage);
|
|
885
|
+
await sessionTerminatedPromise;
|
|
886
|
+
}
|
|
887
|
+
else {
|
|
888
|
+
this.socket.send(terminateSessionMessage);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
if (this.socket?.removeAllListeners)
|
|
892
|
+
this.socket.removeAllListeners();
|
|
893
|
+
this.socket.close();
|
|
894
|
+
}
|
|
895
|
+
this.listeners = {};
|
|
896
|
+
this.socket = undefined;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
class StreamingTranscriberFactory extends BaseService {
|
|
901
|
+
constructor(params) {
|
|
902
|
+
super(params);
|
|
903
|
+
this.baseServiceParams = params;
|
|
904
|
+
}
|
|
905
|
+
transcriber(params) {
|
|
906
|
+
const serviceParams = { ...params };
|
|
907
|
+
if (!serviceParams.token && !serviceParams.apiKey) {
|
|
908
|
+
serviceParams.apiKey = this.baseServiceParams.apiKey;
|
|
909
|
+
}
|
|
910
|
+
return new StreamingTranscriber(serviceParams);
|
|
911
|
+
}
|
|
912
|
+
async createTemporaryToken(params) {
|
|
913
|
+
const searchParams = new URLSearchParams();
|
|
914
|
+
// Add each param to the search params
|
|
915
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
916
|
+
if (value !== undefined && value !== null) {
|
|
917
|
+
searchParams.append(key, String(value));
|
|
918
|
+
}
|
|
919
|
+
});
|
|
920
|
+
const queryString = searchParams.toString();
|
|
921
|
+
const url = queryString ? `/v3/token?${queryString}` : "/v3/token";
|
|
922
|
+
const data = await this.fetchJson(url, {
|
|
923
|
+
method: "GET",
|
|
924
|
+
});
|
|
925
|
+
return data.token;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
|
|
730
929
|
const defaultBaseUrl = "https://api.assemblyai.com";
|
|
930
|
+
const defaultStreamingUrl = "https://streaming.assemblyai.com";
|
|
731
931
|
class AssemblyAI {
|
|
732
932
|
/**
|
|
733
933
|
* Create a new AssemblyAI client.
|
|
@@ -742,7 +942,11 @@ class AssemblyAI {
|
|
|
742
942
|
this.transcripts = new TranscriptService(params, this.files);
|
|
743
943
|
this.lemur = new LemurService(params);
|
|
744
944
|
this.realtime = new RealtimeTranscriberFactory(params);
|
|
945
|
+
this.streaming = new StreamingTranscriberFactory({
|
|
946
|
+
...params,
|
|
947
|
+
baseUrl: params.streamingBaseUrl || defaultStreamingUrl,
|
|
948
|
+
});
|
|
745
949
|
}
|
|
746
950
|
}
|
|
747
951
|
|
|
748
|
-
export { AssemblyAI, FileService, LemurService, RealtimeService, RealtimeServiceFactory, RealtimeTranscriber, RealtimeTranscriberFactory, TranscriptService };
|
|
952
|
+
export { AssemblyAI, FileService, LemurService, RealtimeService, RealtimeServiceFactory, RealtimeTranscriber, RealtimeTranscriberFactory, StreamingTranscriber, TranscriptService };
|