assemblyai 3.0.1 → 3.1.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.
@@ -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 Parameters The type of the parameters required to create the resource.
4
+ * @template Params The type of the parameters required to create the resource.
5
5
  */
6
- interface Createable<T, Parameters, Options = Record<string, unknown>> {
6
+ interface Createable<T, Params, Options = Record<string, unknown>> {
7
7
  /**
8
8
  * Create a new resource.
9
9
  * @param params The parameters of the new resource.
10
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: Parameters, options?: Options): Promise<T>;
13
+ create(params: Params, options?: Options): Promise<T>;
14
14
  }
15
15
  /**
16
16
  * Interface for classes that can retrieve resources.
@@ -2,5 +2,5 @@ type BaseServiceParams = {
2
2
  apiKey: string;
3
3
  baseUrl?: string;
4
4
  };
5
- export type * from "./abstractions";
5
+ export * from "./abstractions";
6
6
  export type { BaseServiceParams };
@@ -1,5 +1,39 @@
1
- export type CreateTranscriptOptions = {
2
- poll?: boolean;
1
+ import { FileUploadParams } from "../files";
2
+ import { TranscriptParams } from "../openapi.generated";
3
+ export type PollingOptions = {
4
+ /**
5
+ * The amount of time to wait between polling requests.
6
+ * @default 3000 or every 3 seconds
7
+ */
3
8
  pollingInterval?: number;
9
+ /**
10
+ * The maximum amount of time to wait for the transcript to be ready.
11
+ * @default -1 which means wait forever
12
+ */
4
13
  pollingTimeout?: number;
5
14
  };
15
+ export type CreateTranscriptOptions = {
16
+ /**
17
+ * Whether to poll the transcript until it is ready.
18
+ * @default true
19
+ */
20
+ poll?: boolean;
21
+ } & PollingOptions;
22
+ /**
23
+ * The audio to transcribe. This can be a public URL, a local file path, a readable file stream, or a file buffer.
24
+ */
25
+ export type AudioToTranscribe = FileUploadParams;
26
+ /**
27
+ * The parameters to transcribe an audio file.
28
+ */
29
+ export type TranscribeParams = {
30
+ audio: AudioToTranscribe;
31
+ } & Omit<TranscriptParams, "audio_url">;
32
+ /**
33
+ * The parameters to start the transcription of an audio file.
34
+ */
35
+ export type SubmitParams = TranscribeParams;
36
+ /**
37
+ * The options to transcribe an audio file, including polling options.
38
+ */
39
+ export type TranscribeOptions = PollingOptions;
package/package.json CHANGED
@@ -1,11 +1,17 @@
1
1
  {
2
2
  "name": "assemblyai",
3
- "version": "3.0.1",
3
+ "version": "3.1.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",
7
7
  "types": "dist/index.d.ts",
8
8
  "typings": "dist/index.d.ts",
9
+ "exports": {
10
+ "default": "./dist/index.js",
11
+ "require": "./dist/index.js",
12
+ "import": "./dist/index.esm.js",
13
+ "node": "./dist/index.js"
14
+ },
9
15
  "repository": {
10
16
  "type": "git",
11
17
  "url": "git+https://github.com/AssemblyAI/assemblyai-node-sdk.git"
@@ -18,10 +24,10 @@
18
24
  "scripts": {
19
25
  "build": "pnpm clean && pnpm rollup -c",
20
26
  "clean": "rimraf dist",
21
- "lint": "eslint -c .eslintrc.json 'src/**/*'",
27
+ "lint": "eslint -c .eslintrc.json '{src,tests}/**/*.{js,ts}'",
22
28
  "test": "pnpm lint && pnpm test:unit",
23
29
  "test:unit": "jest --config jest.config.rollup.ts",
24
- "format": "prettier --write 'src/**/*.ts'",
30
+ "format": "prettier '**/*' --write",
25
31
  "generate-types": "tsx ./scripts/generate-types.ts && pnpm format",
26
32
  "copybara:dry-run": "./copybara.sh dry_run --init-history",
27
33
  "copybara:pr": "./copybara.sh sync_out --init-history"
@@ -38,8 +44,7 @@
38
44
  "homepage": "https://www.assemblyai.com/docs",
39
45
  "files": [
40
46
  "dist",
41
- "src",
42
- "types"
47
+ "src"
43
48
  ],
44
49
  "devDependencies": {
45
50
  "@types/jest": "^29.5.5",
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export type * from "./types";
1
+ export * from "./types";
2
2
  export * from "./services";
@@ -2,7 +2,7 @@
2
2
  // to keep the assemblyai module more compatible. Some fs polyfills don't include `createReadStream`.
3
3
  import fs from "fs";
4
4
  import { BaseService } from "../base";
5
- import { UploadedFile, FileUploadParameters, FileUploadData } from "../..";
5
+ import { UploadedFile, FileUploadParams, FileUploadData } from "../..";
6
6
 
7
7
  export class FileService extends BaseService {
8
8
  /**
@@ -10,7 +10,7 @@ export class FileService extends BaseService {
10
10
  * @param input The local file path to upload, or a stream or buffer of the file to upload.
11
11
  * @return A promise that resolves to the uploaded file URL.
12
12
  */
13
- async upload(input: FileUploadParameters): Promise<string> {
13
+ async upload(input: FileUploadParams): Promise<string> {
14
14
  let fileData: FileUploadData;
15
15
  if (typeof input === "string") fileData = fs.createReadStream(input);
16
16
  else fileData = input;
@@ -1,8 +1,8 @@
1
1
  import {
2
- LemurSummaryParameters,
3
- LemurActionItemsParameters,
4
- LemurQuestionAnswerParameters,
5
- LemurTaskParameters,
2
+ LemurSummaryParams,
3
+ LemurActionItemsParams,
4
+ LemurQuestionAnswerParams,
5
+ LemurTaskParams,
6
6
  LemurSummaryResponse,
7
7
  LemurQuestionAnswerResponse,
8
8
  LemurActionItemsResponse,
@@ -12,7 +12,7 @@ import {
12
12
  import { BaseService } from "../base";
13
13
 
14
14
  export class LemurService extends BaseService {
15
- summary(params: LemurSummaryParameters): Promise<LemurSummaryResponse> {
15
+ summary(params: LemurSummaryParams): Promise<LemurSummaryResponse> {
16
16
  return this.fetchJson<LemurSummaryResponse>("/lemur/v3/generate/summary", {
17
17
  method: "POST",
18
18
  body: JSON.stringify(params),
@@ -20,7 +20,7 @@ export class LemurService extends BaseService {
20
20
  }
21
21
 
22
22
  questionAnswer(
23
- params: LemurQuestionAnswerParameters
23
+ params: LemurQuestionAnswerParams
24
24
  ): Promise<LemurQuestionAnswerResponse> {
25
25
  return this.fetchJson<LemurQuestionAnswerResponse>(
26
26
  "/lemur/v3/generate/question-answer",
@@ -32,7 +32,7 @@ export class LemurService extends BaseService {
32
32
  }
33
33
 
34
34
  actionItems(
35
- params: LemurActionItemsParameters
35
+ params: LemurActionItemsParams
36
36
  ): Promise<LemurActionItemsResponse> {
37
37
  return this.fetchJson<LemurActionItemsResponse>(
38
38
  "/lemur/v3/generate/action-items",
@@ -43,7 +43,7 @@ export class LemurService extends BaseService {
43
43
  );
44
44
  }
45
45
 
46
- task(params: LemurTaskParameters): Promise<LemurTaskResponse> {
46
+ task(params: LemurTaskParams): Promise<LemurTaskResponse> {
47
47
  return this.fetchJson<LemurTaskResponse>("/lemur/v3/generate/task", {
48
48
  method: "POST",
49
49
  body: JSON.stringify(params),
@@ -4,7 +4,7 @@ import {
4
4
  SentencesResponse,
5
5
  Transcript,
6
6
  TranscriptList,
7
- CreateTranscriptParameters,
7
+ TranscriptParams,
8
8
  CreateTranscriptOptions,
9
9
  Createable,
10
10
  Deletable,
@@ -12,16 +12,20 @@ import {
12
12
  Retrieveable,
13
13
  SubtitleFormat,
14
14
  RedactedAudioResponse,
15
- TranscriptListParameters,
15
+ ListTranscriptParams,
16
16
  WordSearchResponse,
17
17
  BaseServiceParams,
18
+ PollingOptions,
19
+ TranscribeParams,
20
+ TranscribeOptions,
21
+ SubmitParams,
18
22
  } from "../..";
19
23
  import { FileService } from "../files";
20
24
 
21
25
  export class TranscriptService
22
26
  extends BaseService
23
27
  implements
24
- Createable<Transcript, CreateTranscriptParameters, CreateTranscriptOptions>,
28
+ Createable<Transcript, TranscriptParams, CreateTranscriptOptions>,
25
29
  Retrieveable<Transcript>,
26
30
  Deletable<Transcript>,
27
31
  Listable<TranscriptList>
@@ -30,14 +34,58 @@ export class TranscriptService
30
34
  super(params);
31
35
  }
32
36
 
37
+ /**
38
+ * Transcribe an audio file. This will create a transcript and wait until the transcript status is "completed" or "error".
39
+ * @param params The parameters to transcribe an audio file.
40
+ * @param options The options to transcribe an audio file.
41
+ * @returns A promise that resolves to the transcript. The transcript status is "completed" or "error".
42
+ */
43
+ async transcribe(
44
+ params: TranscribeParams,
45
+ options?: TranscribeOptions
46
+ ): Promise<Transcript> {
47
+ const transcript = await this.submit(params);
48
+ return await this.waitUntilReady(transcript.id, options);
49
+ }
50
+
51
+ /**
52
+ * Submits a transcription job for an audio file. This will not wait until the transcript status is "completed" or "error".
53
+ * @param params The parameters to start the transcription of an audio file.
54
+ * @returns A promise that resolves to the queued transcript.
55
+ */
56
+ async submit(params: SubmitParams): Promise<Transcript> {
57
+ const { audio, ...createParams } = params;
58
+ let audioUrl;
59
+ if (typeof audio === "string") {
60
+ const path = getPath(audio);
61
+ if (path !== null) {
62
+ // audio is local path, upload local file
63
+ audioUrl = await this.files.upload(path);
64
+ } else {
65
+ // audio is not a local path, assume it's a URL
66
+ audioUrl = audio;
67
+ }
68
+ } else {
69
+ // audio is of uploadable type
70
+ audioUrl = await this.files.upload(audio);
71
+ }
72
+
73
+ const data = await this.fetchJson<Transcript>("/v2/transcript", {
74
+ method: "POST",
75
+ body: JSON.stringify({ ...createParams, audio_url: audioUrl }),
76
+ });
77
+ return data;
78
+ }
79
+
33
80
  /**
34
81
  * Create a transcript.
35
82
  * @param params The parameters to create a transcript.
36
83
  * @param options The options used for creating the new transcript.
37
- * @returns A promise that resolves to the newly created transcript.
84
+ * @returns A promise that resolves to the transcript.
85
+ * @deprecated Use `transcribe` instead to transcribe a audio file that includes polling, or `submit` to transcribe a audio file without polling.
38
86
  */
39
87
  async create(
40
- params: CreateTranscriptParameters,
88
+ params: TranscriptParams,
41
89
  options?: CreateTranscriptOptions
42
90
  ): Promise<Transcript> {
43
91
  const path = getPath(params.audio_url);
@@ -52,16 +100,24 @@ export class TranscriptService
52
100
  });
53
101
 
54
102
  if (options?.poll ?? true) {
55
- return await this.poll(data.id, options);
103
+ return await this.waitUntilReady(data.id, options);
56
104
  }
57
105
 
58
106
  return data;
59
107
  }
60
108
 
61
- private async poll(
109
+ /**
110
+ * Wait until the transcript ready, either the status is "completed" or "error".
111
+ * @param transcriptId The ID of the transcript.
112
+ * @param options The options to wait until the transcript is ready.
113
+ * @returns A promise that resolves to the transcript. The transcript status is "completed" or "error".
114
+ */
115
+ async waitUntilReady(
62
116
  transcriptId: string,
63
- options?: CreateTranscriptOptions
117
+ options?: PollingOptions
64
118
  ): Promise<Transcript> {
119
+ const pollingInterval = options?.pollingInterval ?? 3_000;
120
+ const pollingTimeout = options?.pollingTimeout ?? -1;
65
121
  const startTime = Date.now();
66
122
  // eslint-disable-next-line no-constant-condition
67
123
  while (true) {
@@ -69,14 +125,12 @@ export class TranscriptService
69
125
  if (transcript.status === "completed" || transcript.status === "error") {
70
126
  return transcript;
71
127
  } else if (
72
- Date.now() - startTime <
73
- (options?.pollingTimeout ?? 180_000)
128
+ pollingTimeout > 0 &&
129
+ Date.now() - startTime > pollingTimeout
74
130
  ) {
75
- await new Promise((resolve) =>
76
- setTimeout(resolve, options?.pollingInterval ?? 3_000)
77
- );
78
- } else {
79
131
  throw new Error("Polling timeout");
132
+ } else {
133
+ await new Promise((resolve) => setTimeout(resolve, pollingInterval));
80
134
  }
81
135
  }
82
136
  }
@@ -95,7 +149,7 @@ export class TranscriptService
95
149
  * @param parameters The parameters to filter the transcript list by, or the URL to retrieve the transcript list from.
96
150
  */
97
151
  async list(
98
- parameters?: TranscriptListParameters | string
152
+ parameters?: ListTranscriptParams | string
99
153
  ): Promise<TranscriptList> {
100
154
  let url = "/v2/transcript";
101
155
  if (typeof parameters === "string") {
@@ -104,7 +158,7 @@ export class TranscriptService
104
158
  url = `${url}?${new URLSearchParams(
105
159
  Object.keys(parameters).map((key) => [
106
160
  key,
107
- parameters[key as keyof TranscriptListParameters]?.toString() || "",
161
+ parameters[key as keyof ListTranscriptParams]?.toString() || "",
108
162
  ])
109
163
  )}`;
110
164
  }
@@ -136,8 +190,9 @@ export class TranscriptService
136
190
  * @return A promise that resolves to the sentences.
137
191
  */
138
192
  wordSearch(id: string, words: string[]): Promise<WordSearchResponse> {
193
+ const params = new URLSearchParams({ words: words.join(",") });
139
194
  return this.fetchJson<WordSearchResponse>(
140
- `/v2/transcript/${id}/word-search?words=${JSON.stringify(words)}`
195
+ `/v2/transcript/${id}/word-search?${params.toString()}`
141
196
  );
142
197
  }
143
198
 
@@ -165,10 +220,21 @@ export class TranscriptService
165
220
  * Retrieve subtitles of a transcript.
166
221
  * @param id The identifier of the transcript.
167
222
  * @param format The format of the subtitles.
223
+ * @param chars_per_caption The maximum number of characters per caption.
168
224
  * @return A promise that resolves to the subtitles text.
169
225
  */
170
- async subtitles(id: string, format: SubtitleFormat = "srt"): Promise<string> {
171
- const response = await this.fetch(`/v2/transcript/${id}/${format}`);
226
+ async subtitles(
227
+ id: string,
228
+ format: SubtitleFormat = "srt",
229
+ chars_per_caption?: number
230
+ ): Promise<string> {
231
+ let url = `/v2/transcript/${id}/${format}`;
232
+ if (chars_per_caption) {
233
+ const params = new URLSearchParams();
234
+ params.set("chars_per_caption", chars_per_caption.toString());
235
+ url += `?${params.toString()}`;
236
+ }
237
+ const response = await this.fetch(url);
172
238
  return await response.text();
173
239
  }
174
240
 
@@ -14,19 +14,19 @@ type OneOf<T extends any[]> = T extends [infer Only]
14
14
  : never;
15
15
 
16
16
  export type AudioData = {
17
- /** @description Raw audio data, base64 encoded. This can be the raw data recorded directly from a microphone or read from an audio file. */
17
+ /** @description Base64 encoded raw audio data */
18
18
  audio_data: string;
19
19
  };
20
20
 
21
21
  export type FinalTranscript = RealtimeBaseTranscript & {
22
22
  /**
23
- * @description Describes the type of message.
23
+ * @description Describes the type of message
24
24
  * @constant
25
25
  */
26
26
  message_type: "FinalTranscript";
27
- /** @description Whether the text has been punctuated and cased. */
27
+ /** @description Whether the text is punctuated and cased */
28
28
  punctuated: boolean;
29
- /** @description Whether the text has been formatted (e.g. Dollar -> $) */
29
+ /** @description Whether the text is formatted, for example Dollar -> $ */
30
30
  text_formatted: boolean;
31
31
  };
32
32
 
@@ -39,32 +39,35 @@ export type MessageType =
39
39
 
40
40
  export type PartialTranscript = RealtimeBaseTranscript & {
41
41
  /**
42
- * @description Describes the type of message.
42
+ * @description Describes the type of message
43
43
  * @constant
44
44
  */
45
45
  message_type: "PartialTranscript";
46
46
  };
47
47
 
48
48
  export type RealtimeBaseMessage = {
49
- /** @description Describes the type of the message. */
49
+ /** @description Describes the type of the message */
50
50
  message_type: MessageType;
51
51
  };
52
52
 
53
53
  export type RealtimeBaseTranscript = {
54
- /** @description End time of audio sample relative to session start, in milliseconds. */
54
+ /** @description End time of audio sample relative to session start, in milliseconds */
55
55
  audio_end: number;
56
- /** @description Start time of audio sample relative to session start, in milliseconds. */
56
+ /** @description Start time of audio sample relative to session start, in milliseconds */
57
57
  audio_start: number;
58
58
  /**
59
59
  * Format: double
60
- * @description The confidence score of the entire transcription, between 0 and 1.
60
+ * @description The confidence score of the entire transcription, between 0 and 1
61
61
  */
62
62
  confidence: number;
63
- /** @description The timestamp for the partial transcript. */
63
+ /** @description The timestamp for the partial transcript */
64
64
  created: Date;
65
- /** @description The partial transcript for your audio. */
65
+ /** @description The partial transcript for your audio */
66
66
  text: string;
67
- /** @description An array of objects, with the information for each word in the transcription text. Includes the start/end time (in milliseconds) of the word, the confidence score of the word, and the text (i.e. the word itself). */
67
+ /**
68
+ * @description An array of objects, with the information for each word in the transcription text.
69
+ * Includes the start and end time of the word in milliseconds, the confidence score of the word, and the text, which is the word itself.
70
+ */
68
71
  words: Word[];
69
72
  };
70
73
 
@@ -85,27 +88,27 @@ export type RealtimeTranscript = PartialTranscript | FinalTranscript;
85
88
  export type RealtimeTranscriptType = "PartialTranscript" | "FinalTranscript";
86
89
 
87
90
  export type SessionBegins = RealtimeBaseMessage & {
88
- /** @description Timestamp when this session will expire. */
91
+ /** @description Timestamp when this session will expire */
89
92
  expires_at: Date;
90
93
  /**
91
- * @description Describes the type of the message.
94
+ * @description Describes the type of the message
92
95
  * @constant
93
96
  */
94
97
  message_type: "SessionBegins";
95
- /** @description Unique identifier for the established session. */
98
+ /** @description Unique identifier for the established session */
96
99
  session_id: string;
97
100
  };
98
101
 
99
102
  export type SessionTerminated = RealtimeBaseMessage & {
100
103
  /**
101
- * @description Describes the type of the message.
104
+ * @description Describes the type of the message
102
105
  * @constant
103
106
  */
104
107
  message_type: "SessionTerminated";
105
108
  };
106
109
 
107
110
  export type TerminateSession = RealtimeBaseMessage & {
108
- /** @description A boolean value to communicate that you wish to end your real-time session forever. */
111
+ /** @description Set to true to end your real-time session forever */
109
112
  terminate_session: boolean;
110
113
  };
111
114
 
@@ -0,0 +1,78 @@
1
+ import { FileUploadParams } from "./files";
2
+ import {
3
+ CreateRealtimeTemporaryTokenParams,
4
+ LemurActionItemsParams,
5
+ LemurBaseParams,
6
+ LemurQuestionAnswerParams,
7
+ LemurSummaryParams,
8
+ LemurTaskParams,
9
+ ListTranscriptParams,
10
+ TranscriptOptionalParams,
11
+ TranscriptParams,
12
+ } from "./openapi.generated";
13
+ import { SubmitParams, TranscribeParams } from "./transcripts";
14
+
15
+ /**
16
+ * @deprecated
17
+ * Use`FileUploadParams` instead.
18
+ */
19
+ export type FileUploadParameters = FileUploadParams;
20
+
21
+ /**
22
+ * @deprecated
23
+ * Use`TranscribeParams` instead.
24
+ */
25
+ export type TranscribeParameters = TranscribeParams;
26
+
27
+ /**
28
+ * @deprecated
29
+ * Use`SubmitParams` instead.
30
+ */
31
+ export type SubmitParameters = SubmitParams;
32
+
33
+ /**
34
+ * @deprecated
35
+ * Use`CreateRealtimeTemporaryTokenParams` instead.
36
+ */
37
+ export type CreateRealtimeTemporaryTokenParameters =
38
+ CreateRealtimeTemporaryTokenParams;
39
+ /**
40
+ * @deprecated
41
+ * Use`LemurActionItemsParams` instead.
42
+ */
43
+ export type LemurActionItemsParameters = LemurActionItemsParams;
44
+ /**
45
+ * @deprecated
46
+ * Use`LemurBaseParams` instead.
47
+ */
48
+ export type LemurBaseParameters = LemurBaseParams;
49
+ /**
50
+ * @deprecated
51
+ * Use`LemurQuestionAnswerParams` instead.
52
+ */
53
+ export type LemurQuestionAnswerParameters = LemurQuestionAnswerParams;
54
+ /**
55
+ * @deprecated
56
+ * Use`LemurSummaryParams` instead.
57
+ */
58
+ export type LemurSummaryParameters = LemurSummaryParams;
59
+ /**
60
+ * @deprecated
61
+ * Use`LemurTaskParams` instead.
62
+ */
63
+ export type LemurTaskParameters = LemurTaskParams;
64
+ /**
65
+ * @deprecated
66
+ * Use`ListTranscriptParams` instead.
67
+ */
68
+ export type TranscriptListParameters = ListTranscriptParams;
69
+ /**
70
+ * @deprecated
71
+ * Use`TranscriptOptionalParams` instead.
72
+ */
73
+ export type CreateTranscriptOptionalParameters = TranscriptOptionalParams;
74
+ /**
75
+ * @deprecated
76
+ * Use`TranscriptParams` instead.
77
+ */
78
+ export type CreateTranscriptParameters = TranscriptParams;
@@ -1,4 +1,4 @@
1
- type FileUploadParameters = string | FileUploadData;
1
+ type FileUploadParams = string | FileUploadData;
2
2
  type FileUploadData =
3
3
  | NodeJS.ReadableStream
4
4
  | ReadableStream
@@ -8,4 +8,4 @@ type FileUploadData =
8
8
  | ArrayBufferLike
9
9
  | Uint8Array;
10
10
 
11
- export type { FileUploadParameters, FileUploadData };
11
+ export type { FileUploadParams, FileUploadData };
@@ -1,6 +1,7 @@
1
- export type * from "./files";
2
- export type * from "./transcripts";
3
- export type * from "./realtime";
4
- export type * from "./services";
5
- export type * from "./asyncapi.generated";
6
- export type * from "./openapi.generated";
1
+ export * from "./files";
2
+ export * from "./transcripts";
3
+ export * from "./realtime";
4
+ export * from "./services";
5
+ export * from "./asyncapi.generated";
6
+ export * from "./openapi.generated";
7
+ export * from "./deprecated";