assemblyai 2.0.0-beta → 2.0.1
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 +148 -48
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +218 -123
- package/dist/index.js +225 -123
- package/dist/services/base.d.ts +0 -2
- package/dist/services/index.d.ts +7 -9
- package/dist/services/lemur/index.d.ts +6 -6
- package/dist/services/realtime/factory.d.ts +10 -0
- package/dist/services/realtime/index.d.ts +2 -16
- package/dist/services/realtime/service.d.ts +22 -0
- package/dist/services/transcripts/index.d.ts +37 -20
- package/dist/types/asyncapi.generated.d.ts +87 -0
- package/dist/types/index.d.ts +3 -4
- package/dist/types/openapi.generated.d.ts +685 -0
- package/dist/types/realtime/index.d.ts +30 -11
- package/dist/types/services/abstractions.d.ts +8 -36
- package/dist/types/services/index.d.ts +2 -5
- package/dist/types/transcripts/index.d.ts +5 -0
- package/dist/utils/axios.d.ts +3 -0
- package/dist/utils/errors/realtime.d.ts +1 -4
- package/package.json +16 -8
- package/src/index.ts +3 -0
- package/src/services/base.ts +1 -3
- package/src/services/files/index.ts +4 -4
- package/src/services/index.ts +18 -35
- package/src/services/lemur/index.ts +20 -16
- package/src/services/realtime/factory.ts +32 -0
- package/src/services/realtime/index.ts +2 -106
- package/src/services/realtime/service.ts +184 -0
- package/src/services/transcripts/index.ts +85 -63
- package/src/types/asyncapi.generated.ts +124 -0
- package/src/types/index.ts +3 -4
- package/src/types/openapi.generated.ts +834 -0
- package/src/types/realtime/index.ts +53 -13
- package/src/types/services/abstractions.ts +8 -40
- package/src/types/services/index.ts +2 -6
- package/src/types/transcripts/index.ts +5 -0
- package/src/utils/axios.ts +19 -0
- package/src/utils/errors/realtime.ts +5 -18
- package/dist/services/transcripts/redactions.d.ts +0 -14
- package/dist/services/transcripts/subtitles.d.ts +0 -12
- package/dist/types/core/index.d.ts +0 -2
- package/dist/types/core/params.d.ts +0 -2
- package/dist/types/core/segments.d.ts +0 -28
- package/dist/types/core/transcript.d.ts +0 -113
- package/dist/types/lemur/index.d.ts +0 -79
- package/dist/types/models/auto_chapter.d.ts +0 -8
- package/dist/types/models/auto_highlights.d.ts +0 -11
- package/dist/types/models/content_safety.d.ts +0 -25
- package/dist/types/models/entity_detection.d.ts +0 -7
- package/dist/types/models/index.d.ts +0 -8
- package/dist/types/models/pii.d.ts +0 -3
- package/dist/types/models/sentiment_analysis.d.ts +0 -9
- package/dist/types/models/shared.d.ts +0 -3
- package/dist/types/models/summarization.d.ts +0 -3
- package/dist/types/models/topic_detection.d.ts +0 -17
- package/dist/types/shared/index.d.ts +0 -2
- package/dist/types/shared/pagination.d.ts +0 -7
- package/dist/types/shared/timestamp.d.ts +0 -5
- package/src/services/transcripts/redactions.ts +0 -27
- package/src/services/transcripts/subtitles.ts +0 -26
- package/src/types/core/index.ts +0 -2
- package/src/types/core/params.ts +0 -3
- package/src/types/core/segments.ts +0 -29
- package/src/types/core/transcript.ts +0 -159
- package/src/types/lemur/index.ts +0 -97
- package/src/types/models/auto_chapter.ts +0 -9
- package/src/types/models/auto_highlights.ts +0 -14
- package/src/types/models/content_safety.ts +0 -34
- package/src/types/models/entity_detection.ts +0 -8
- package/src/types/models/index.ts +0 -8
- package/src/types/models/pii.ts +0 -32
- package/src/types/models/sentiment_analysis.ts +0 -11
- package/src/types/models/shared.ts +0 -4
- package/src/types/models/summarization.ts +0 -10
- package/src/types/models/topic_detection.ts +0 -21
- package/src/types/shared/index.ts +0 -2
- package/src/types/shared/pagination.ts +0 -8
- package/src/types/shared/timestamp.ts +0 -6
|
@@ -1,17 +1,36 @@
|
|
|
1
|
-
|
|
1
|
+
import { FinalTranscript, PartialTranscript, RealtimeTranscript, RealtimeTranscriptType } from "../asyncapi.generated";
|
|
2
|
+
type CreateRealtimeServiceParams = {
|
|
3
|
+
realtimeUrl?: string;
|
|
2
4
|
sampleRate?: number;
|
|
3
5
|
wordBoost?: string[];
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
} & ({
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
} | {
|
|
9
|
+
token: string;
|
|
10
|
+
});
|
|
11
|
+
type RealtimeServiceParams = {
|
|
12
|
+
realtimeUrl?: string;
|
|
13
|
+
sampleRate?: number;
|
|
14
|
+
wordBoost?: string[];
|
|
15
|
+
} & ({
|
|
16
|
+
apiKey: string;
|
|
17
|
+
} | {
|
|
18
|
+
token: string;
|
|
19
|
+
});
|
|
20
|
+
type RealtimeEvents = "open" | "close" | "transcript" | "transcript.partial" | "transcript.final" | "error";
|
|
21
|
+
type SessionBeginsEventData = {
|
|
22
|
+
sessionId: string;
|
|
23
|
+
expiresAt: Date;
|
|
10
24
|
};
|
|
11
25
|
type RealtimeListeners = {
|
|
12
|
-
|
|
13
|
-
error?: (error: Error) => void;
|
|
14
|
-
open?: () => void;
|
|
26
|
+
open?: (event: SessionBeginsEventData) => void;
|
|
15
27
|
close?: (code: number, reason: string) => void;
|
|
28
|
+
transcript?: (transcript: RealtimeTranscript) => void;
|
|
29
|
+
"transcript.partial"?: (transcript: PartialTranscript) => void;
|
|
30
|
+
"transcript.final"?: (transcript: FinalTranscript) => void;
|
|
31
|
+
error?: (error: Error) => void;
|
|
32
|
+
};
|
|
33
|
+
type RealtimeTokenParams = {
|
|
34
|
+
expires_in: number;
|
|
16
35
|
};
|
|
17
|
-
export type {
|
|
36
|
+
export type { CreateRealtimeServiceParams, RealtimeServiceParams, RealtimeEvents, RealtimeTranscriptType, SessionBeginsEventData, RealtimeListeners, RealtimeTokenParams, };
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Interface for classes that can create resources.
|
|
3
3
|
* @template T The type of the resource.
|
|
4
|
-
* @template
|
|
4
|
+
* @template Parameters The type of the parameters required to create the resource.
|
|
5
5
|
*/
|
|
6
|
-
interface Createable<T,
|
|
6
|
+
interface Createable<T, Parameters, Options = Record<string, any>> {
|
|
7
7
|
/**
|
|
8
8
|
* Create a new resource.
|
|
9
|
-
* @param
|
|
10
|
-
* @param
|
|
9
|
+
* @param params The parameters of the new resource.
|
|
10
|
+
* @param options The options used for creating the new resource.
|
|
11
11
|
* @return A promise that resolves to the newly created resource.
|
|
12
12
|
*/
|
|
13
|
-
create(params:
|
|
13
|
+
create(params: Parameters, options?: Options): Promise<T>;
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* Interface for classes that can retrieve resources.
|
|
@@ -19,11 +19,11 @@ interface Createable<T, Props, Settings = Record<string, any>> {
|
|
|
19
19
|
*/
|
|
20
20
|
interface Retrieveable<T, Id = string> {
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* Get a resource.
|
|
23
23
|
* @param id The identifier of the resource to retrieve.
|
|
24
24
|
* @return A promise that resolves to the retrieved resource.
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
get(id: Id): Promise<T>;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Interface for classes that can delete resources.
|
|
@@ -49,32 +49,4 @@ interface Listable<T, Page = string> {
|
|
|
49
49
|
*/
|
|
50
50
|
list(page?: Page): Promise<T>;
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
* Interface for classes that can connect to resources.
|
|
54
|
-
* @template T The type of the resource's data.
|
|
55
|
-
*/
|
|
56
|
-
interface Connectable<T, Params> {
|
|
57
|
-
/**
|
|
58
|
-
* Connect to a resource.
|
|
59
|
-
* @param event The event to listen for.
|
|
60
|
-
* @param listener The listener for the event.
|
|
61
|
-
*/
|
|
62
|
-
on(event: "data", listener: (data: T) => void): void;
|
|
63
|
-
on(event: "error", listener: (error: Error) => void): void;
|
|
64
|
-
on(event: "open", listener: () => void): void;
|
|
65
|
-
on(event: "close", listener: (code: number, reason: string) => void): void;
|
|
66
|
-
/**
|
|
67
|
-
* Send data to the resource.
|
|
68
|
-
* @param data The data to send.
|
|
69
|
-
*/
|
|
70
|
-
send(data: ArrayBuffer): void;
|
|
71
|
-
/**
|
|
72
|
-
* Connect to the resource.
|
|
73
|
-
*/
|
|
74
|
-
connect(params: Params): void;
|
|
75
|
-
/**
|
|
76
|
-
* Disconnect from the resource.
|
|
77
|
-
*/
|
|
78
|
-
disconnect(): void;
|
|
79
|
-
}
|
|
80
|
-
export type { Createable, Retrieveable, Deletable, Listable, Connectable };
|
|
52
|
+
export type { Createable, Retrieveable, Deletable, Listable };
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
declare enum RealtimeErrorType {
|
|
2
2
|
BadSampleRate = 4000,
|
|
3
3
|
AuthFailed = 4001,
|
|
4
|
-
|
|
5
|
-
FreeAccount = 4003,
|
|
4
|
+
InsufficientFundsOrFreeAccount = 4002,
|
|
6
5
|
NonexistentSessionId = 4004,
|
|
7
6
|
SessionExpired = 4008,
|
|
8
7
|
ClosedSession = 4010,
|
|
@@ -19,8 +18,6 @@ declare enum RealtimeErrorType {
|
|
|
19
18
|
}
|
|
20
19
|
declare const RealtimeErrorMessages: Record<RealtimeErrorType, string>;
|
|
21
20
|
declare class RealtimeError extends Error {
|
|
22
|
-
code: RealtimeErrorType;
|
|
23
|
-
constructor(code: RealtimeErrorType);
|
|
24
21
|
}
|
|
25
22
|
export { RealtimeErrorType, RealtimeErrorMessages };
|
|
26
23
|
export default RealtimeError;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assemblyai",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "The AssemblyAI Node.js SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, as well as the latest LeMUR models.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
"typings": "dist/index.d.ts",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/AssemblyAI/assemblyai-
|
|
11
|
+
"url": "git+https://github.com/AssemblyAI/assemblyai-node-sdk.git"
|
|
12
12
|
},
|
|
13
13
|
"publishConfig": {
|
|
14
|
-
"tag": "
|
|
14
|
+
"tag": "latest",
|
|
15
15
|
"access": "public",
|
|
16
16
|
"registry": "https://registry.npmjs.org/"
|
|
17
17
|
},
|
|
@@ -21,11 +21,17 @@
|
|
|
21
21
|
"lint": "tslint -p tsconfig.json",
|
|
22
22
|
"test": "pnpm lint && pnpm test:unit",
|
|
23
23
|
"test:unit": "jest --config jest.config.rollup.ts",
|
|
24
|
-
"
|
|
24
|
+
"format": "prettier --write 'src/**/*.ts'",
|
|
25
|
+
"generate-types": "tsx ./scripts/generate-types.ts && pnpm format",
|
|
26
|
+
"copybara:dry-run": "./copybara.sh dry_run --init-history",
|
|
27
|
+
"copybara:pr": "./copybara.sh sync_out --init-history"
|
|
25
28
|
},
|
|
26
29
|
"keywords": [
|
|
27
30
|
"AssemblyAI",
|
|
28
|
-
"Speech-to-text"
|
|
31
|
+
"Speech-to-text",
|
|
32
|
+
"Transcription",
|
|
33
|
+
"Audio",
|
|
34
|
+
"LLM"
|
|
29
35
|
],
|
|
30
36
|
"author": "AssemblyAI (https://www.assemblyai.com)",
|
|
31
37
|
"license": "MIT",
|
|
@@ -36,17 +42,20 @@
|
|
|
36
42
|
"types"
|
|
37
43
|
],
|
|
38
44
|
"devDependencies": {
|
|
39
|
-
"@types/jest": "^29.5.
|
|
45
|
+
"@types/jest": "^29.5.5",
|
|
40
46
|
"@types/node": "^20.5.7",
|
|
41
47
|
"@types/ws": "^8.5.5",
|
|
48
|
+
"dotenv": "^16.3.1",
|
|
42
49
|
"eslint": "^8.43.0",
|
|
43
50
|
"i": "^0.3.7",
|
|
44
51
|
"jest": "^29.5.0",
|
|
45
52
|
"jest-cli": "^29.5.0",
|
|
46
53
|
"jest-junit": "^16.0.0",
|
|
47
54
|
"jest-mock-extended": "^3.0.4",
|
|
55
|
+
"jest-websocket-mock": "^2.4.1",
|
|
48
56
|
"mock-socket": "^9.2.1",
|
|
49
57
|
"npm": "^9.7.1",
|
|
58
|
+
"openapi-typescript": "^6.6.1",
|
|
50
59
|
"prettier": "^2.8.8",
|
|
51
60
|
"rimraf": "^5.0.1",
|
|
52
61
|
"rollup": "^3.25.1",
|
|
@@ -55,8 +64,7 @@
|
|
|
55
64
|
"ts-node": "^10.9.1",
|
|
56
65
|
"tslib": "^2.5.3",
|
|
57
66
|
"tslint": "^6.1.3",
|
|
58
|
-
"typescript": "^5.
|
|
59
|
-
"jest-websocket-mock": "^2.4.1"
|
|
67
|
+
"typescript": "^5.2.2"
|
|
60
68
|
},
|
|
61
69
|
"dependencies": {
|
|
62
70
|
"axios": "^1.4.0",
|
package/src/index.ts
CHANGED
package/src/services/base.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { BaseServiceParams } from "@/types";
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Base class for services that communicate with the API.
|
|
@@ -12,5 +11,4 @@ abstract class BaseService {
|
|
|
12
11
|
constructor(protected client: AxiosInstance) {}
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
export type { BaseServiceParams };
|
|
16
14
|
export default BaseService;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { readFile } from "fs/promises";
|
|
2
2
|
import BaseService from "@/services/base";
|
|
3
|
-
import {
|
|
3
|
+
import { UploadedFile } from "@/types";
|
|
4
4
|
|
|
5
5
|
export default class FileService extends BaseService {
|
|
6
6
|
/**
|
|
@@ -9,9 +9,9 @@ export default class FileService extends BaseService {
|
|
|
9
9
|
* @return A promise that resolves to the uploaded file URL.
|
|
10
10
|
*/
|
|
11
11
|
async upload(path: string): Promise<string> {
|
|
12
|
-
const file = await
|
|
12
|
+
const file = await readFile(path);
|
|
13
13
|
|
|
14
|
-
const { data } = await this.client.post<
|
|
14
|
+
const { data } = await this.client.post<UploadedFile>("/v2/upload", file, {
|
|
15
15
|
headers: {
|
|
16
16
|
"Content-Type": "application/octet-stream",
|
|
17
17
|
},
|
package/src/services/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { BaseServiceParams } from "
|
|
3
|
-
import
|
|
4
|
-
import RealtimeService from "./realtime";
|
|
1
|
+
import { createAxiosClient } from "@/utils/axios";
|
|
2
|
+
import { BaseServiceParams } from "@/types";
|
|
3
|
+
import LemurService from "./lemur";
|
|
4
|
+
import { RealtimeService, RealtimeServiceFactory } from "./realtime";
|
|
5
5
|
import TranscriptService from "./transcripts";
|
|
6
6
|
import FileService from "./files";
|
|
7
7
|
|
|
@@ -19,48 +19,31 @@ export default class AssemblyAI {
|
|
|
19
19
|
/**
|
|
20
20
|
* The LeMUR service.
|
|
21
21
|
*/
|
|
22
|
-
public lemur:
|
|
22
|
+
public lemur: LemurService;
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* The realtime service.
|
|
26
26
|
*/
|
|
27
|
-
public realtime:
|
|
27
|
+
public realtime: RealtimeServiceFactory;
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* Create a new AssemblyAI client.
|
|
31
|
-
* @param params The parameters for the service, including the API
|
|
31
|
+
* @param params The parameters for the service, including the API key and base URL, if any.
|
|
32
32
|
*/
|
|
33
33
|
constructor(params: BaseServiceParams) {
|
|
34
34
|
params.baseUrl = params.baseUrl || "https://api.assemblyai.com";
|
|
35
|
-
const client =
|
|
35
|
+
const client = createAxiosClient(params);
|
|
36
36
|
this.files = new FileService(client);
|
|
37
37
|
this.transcripts = new TranscriptService(client, this.files);
|
|
38
|
-
this.lemur = new
|
|
39
|
-
this.realtime = (
|
|
40
|
-
if (!baseUrl) {
|
|
41
|
-
const url = new URL(params.baseUrl as string);
|
|
42
|
-
url.protocol = "wss:";
|
|
43
|
-
baseUrl = url.toString();
|
|
44
|
-
}
|
|
45
|
-
return new RealtimeService({
|
|
46
|
-
token: params.token,
|
|
47
|
-
baseUrl,
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
private createClient(params: BaseServiceParams) {
|
|
53
|
-
const client = axios.create({
|
|
54
|
-
baseURL: params.baseUrl,
|
|
55
|
-
headers: { Authorization: params.token },
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
client.interceptors.response.use(null, (error) => {
|
|
59
|
-
if (isAxiosError(error) && error.response?.data?.error) {
|
|
60
|
-
return Promise.reject(new Error(error.response.data.error));
|
|
61
|
-
}
|
|
62
|
-
return Promise.reject(error);
|
|
63
|
-
});
|
|
64
|
-
return client;
|
|
38
|
+
this.lemur = new LemurService(client);
|
|
39
|
+
this.realtime = new RealtimeServiceFactory(client, params);
|
|
65
40
|
}
|
|
66
41
|
}
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
LemurService,
|
|
45
|
+
RealtimeServiceFactory,
|
|
46
|
+
RealtimeService,
|
|
47
|
+
TranscriptService,
|
|
48
|
+
FileService,
|
|
49
|
+
};
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
LemurSummaryParameters,
|
|
3
|
+
LemurActionItemsParameters,
|
|
4
|
+
LemurQuestionAnswerParameters,
|
|
5
|
+
LemurTaskParameters,
|
|
6
|
+
LemurSummaryResponse,
|
|
7
|
+
LemurQuestionAnswerResponse,
|
|
8
|
+
LemurActionItemsResponse,
|
|
9
|
+
LemurTaskResponse,
|
|
8
10
|
} from "@/types";
|
|
9
11
|
import BaseService from "@/services/base";
|
|
10
12
|
|
|
11
|
-
export default class
|
|
12
|
-
async summary(params:
|
|
13
|
-
const { data } = await this.client.post<
|
|
13
|
+
export default class LemurService extends BaseService {
|
|
14
|
+
async summary(params: LemurSummaryParameters): Promise<LemurSummaryResponse> {
|
|
15
|
+
const { data } = await this.client.post<LemurSummaryResponse>(
|
|
14
16
|
"/lemur/v3/generate/summary",
|
|
15
17
|
params
|
|
16
18
|
);
|
|
@@ -18,25 +20,27 @@ export default class LeMURService extends BaseService {
|
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
async questionAnswer(
|
|
21
|
-
params:
|
|
22
|
-
): Promise<
|
|
23
|
-
const { data } = await this.client.post<
|
|
23
|
+
params: LemurQuestionAnswerParameters
|
|
24
|
+
): Promise<LemurQuestionAnswerResponse> {
|
|
25
|
+
const { data } = await this.client.post<LemurQuestionAnswerResponse>(
|
|
24
26
|
"/lemur/v3/generate/question-answer",
|
|
25
27
|
params
|
|
26
28
|
);
|
|
27
29
|
return data;
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
async actionItems(
|
|
31
|
-
|
|
32
|
+
async actionItems(
|
|
33
|
+
params: LemurActionItemsParameters
|
|
34
|
+
): Promise<LemurActionItemsResponse> {
|
|
35
|
+
const { data } = await this.client.post<LemurActionItemsResponse>(
|
|
32
36
|
"/lemur/v3/generate/action-items",
|
|
33
37
|
params
|
|
34
38
|
);
|
|
35
39
|
return data;
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
async task(params:
|
|
39
|
-
const { data } = await this.client.post<
|
|
42
|
+
async task(params: LemurTaskParameters): Promise<LemurTaskResponse> {
|
|
43
|
+
const { data } = await this.client.post<LemurTaskResponse>(
|
|
40
44
|
"/lemur/v3/generate/task",
|
|
41
45
|
params
|
|
42
46
|
);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BaseServiceParams,
|
|
3
|
+
RealtimeTokenParams,
|
|
4
|
+
CreateRealtimeServiceParams,
|
|
5
|
+
RealtimeServiceParams,
|
|
6
|
+
} from "@/types";
|
|
7
|
+
import { AxiosInstance } from "axios";
|
|
8
|
+
import { RealtimeService } from "./service";
|
|
9
|
+
|
|
10
|
+
export class RealtimeServiceFactory {
|
|
11
|
+
constructor(
|
|
12
|
+
private client: AxiosInstance,
|
|
13
|
+
private params: BaseServiceParams
|
|
14
|
+
) {}
|
|
15
|
+
|
|
16
|
+
createService(params?: CreateRealtimeServiceParams): RealtimeService {
|
|
17
|
+
if (!params) params = { apiKey: this.params.apiKey };
|
|
18
|
+
else if (!("token" in params) && !params.apiKey) {
|
|
19
|
+
params.apiKey = this.params.apiKey;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return new RealtimeService(params as RealtimeServiceParams);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async createTemporaryToken(params: RealtimeTokenParams) {
|
|
26
|
+
const response = await this.client.post<{ token: string }>(
|
|
27
|
+
"/v2/realtime/token",
|
|
28
|
+
params
|
|
29
|
+
);
|
|
30
|
+
return response.data.token;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -1,106 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Connectable,
|
|
4
|
-
RealtimeConnectionParams,
|
|
5
|
-
RealtimeListener,
|
|
6
|
-
RealtimeListeners,
|
|
7
|
-
RealtimeTranscript,
|
|
8
|
-
} from "@/types";
|
|
9
|
-
import { RealtimeError, RealtimeErrorType } from "@/utils/errors";
|
|
10
|
-
import WebSocket, { type CloseEvent, type MessageEvent } from "ws";
|
|
11
|
-
|
|
12
|
-
export default class RealtimeService
|
|
13
|
-
implements Connectable<RealtimeTranscript, RealtimeConnectionParams>
|
|
14
|
-
{
|
|
15
|
-
private params: BaseServiceParams;
|
|
16
|
-
private socket?: WebSocket;
|
|
17
|
-
private listeners: RealtimeListeners = {};
|
|
18
|
-
|
|
19
|
-
constructor(params: BaseServiceParams) {
|
|
20
|
-
this.params = params;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
private connectionUrl(
|
|
24
|
-
connectionParams: Required<RealtimeConnectionParams>
|
|
25
|
-
): URL {
|
|
26
|
-
if (!this.params.baseUrl) {
|
|
27
|
-
throw new Error("Missing base URL");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const url = new URL(this.params.baseUrl);
|
|
31
|
-
|
|
32
|
-
if (url.protocol !== "wss:") {
|
|
33
|
-
throw new Error("Invalid protocol, must be wss");
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const searchParams = new URLSearchParams();
|
|
37
|
-
searchParams.set("sample_rate", connectionParams.sampleRate.toString());
|
|
38
|
-
searchParams.set("word_boost", connectionParams.wordBoost.join(","));
|
|
39
|
-
url.search = searchParams.toString();
|
|
40
|
-
|
|
41
|
-
return url;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
on(event: "data", listener: (transcript: RealtimeTranscript) => void): void;
|
|
45
|
-
on(event: "error", listener: (error: Error) => void): void;
|
|
46
|
-
on(event: "open", listener: () => void): void;
|
|
47
|
-
on(event: "close", listener: (code: number, reason: string) => void): void;
|
|
48
|
-
on(event: RealtimeListener, listener: (...args: any[]) => void) {
|
|
49
|
-
this.listeners[event] = listener;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
connect(connectionParams: RealtimeConnectionParams) {
|
|
53
|
-
if (this.socket) {
|
|
54
|
-
throw new Error("Already connected");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const url = this.connectionUrl({
|
|
58
|
-
...connectionParams,
|
|
59
|
-
sampleRate: connectionParams.sampleRate || 16000,
|
|
60
|
-
wordBoost: connectionParams.wordBoost || [],
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
this.socket = new WebSocket(url.toString(), {
|
|
64
|
-
headers: {
|
|
65
|
-
Authorization: this.params.token,
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
this.socket.onopen = () => this.listeners.open?.();
|
|
70
|
-
this.socket.onclose = ({ code, reason }: CloseEvent) =>
|
|
71
|
-
this.listeners.close?.(code, reason);
|
|
72
|
-
|
|
73
|
-
this.socket.onerror = (error) => {
|
|
74
|
-
const code = error.message as unknown as RealtimeErrorType;
|
|
75
|
-
const realtimeError = new RealtimeError(code);
|
|
76
|
-
this.listeners.error?.(realtimeError);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
this.socket.onmessage = ({ data }: MessageEvent) => {
|
|
80
|
-
const message = JSON.parse(data.toString()) as RealtimeTranscript;
|
|
81
|
-
this.listeners.data?.(message);
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
send(data: ArrayBuffer) {
|
|
86
|
-
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
|
|
87
|
-
throw new Error("Socket is not open for communication");
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const payload = {
|
|
91
|
-
audio_data: Buffer.from(data).toString("base64"),
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
this.socket.send(JSON.stringify(payload));
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
disconnect() {
|
|
98
|
-
if (this.socket) {
|
|
99
|
-
this.socket.removeAllListeners();
|
|
100
|
-
this.socket.close();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
this.listeners = {};
|
|
104
|
-
this.socket = undefined;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
1
|
+
export * from "@/services/realtime/factory";
|
|
2
|
+
export * from "@/services/realtime/service";
|