@speechweave/node 1.0.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/LICENSE +21 -0
- package/README.md +116 -0
- package/dist/index.cjs +878 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +375 -0
- package/dist/index.d.ts +375 -0
- package/dist/index.js +830 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import * as node_fs from 'node:fs';
|
|
2
|
+
import { ReadStream } from 'node:fs';
|
|
3
|
+
|
|
4
|
+
/** Job lifecycle status. Terminal: completed, failed, cancelled. */
|
|
5
|
+
type PublicJobStatus = "queued" | "processing" | "completed" | "failed" | "cancelled" | string;
|
|
6
|
+
/**
|
|
7
|
+
* Processing mode.
|
|
8
|
+
* API defaults to deferred when omitted. `asynchronous` is treated as deferred.
|
|
9
|
+
* Synchronous has an API size cap (default 512 MiB); use deferred for larger files.
|
|
10
|
+
*/
|
|
11
|
+
type ServiceMode = "synchronous" | "asynchronous" | "deferred" | string;
|
|
12
|
+
interface V1Job {
|
|
13
|
+
/** Id from createJob / transcribeFile. */
|
|
14
|
+
id: string;
|
|
15
|
+
status: PublicJobStatus;
|
|
16
|
+
model?: string | null;
|
|
17
|
+
service_mode?: ServiceMode | null;
|
|
18
|
+
/** Two-letter ISO language code (e.g. 'en', 'es'). */
|
|
19
|
+
language?: string | null;
|
|
20
|
+
/** Full text when status is completed; otherwise null/absent. */
|
|
21
|
+
transcript?: string | null;
|
|
22
|
+
/** Audio duration in seconds, when known. */
|
|
23
|
+
duration?: number | null;
|
|
24
|
+
created_at?: string | null;
|
|
25
|
+
completed_at?: string | null;
|
|
26
|
+
/** Failure message when status is failed. */
|
|
27
|
+
error?: string | null;
|
|
28
|
+
/** 0–100 while processing, when the API reports it. */
|
|
29
|
+
progress?: number | null;
|
|
30
|
+
/** Coarse pipeline stage label while processing. */
|
|
31
|
+
stage?: string | null;
|
|
32
|
+
}
|
|
33
|
+
interface PresignResponse {
|
|
34
|
+
/** Short-lived URL for a PUT of the audio bytes. */
|
|
35
|
+
upload_url: string;
|
|
36
|
+
/** Pass to createJob as object_key after a successful PUT. */
|
|
37
|
+
object_key: string;
|
|
38
|
+
/** Seconds until upload_url expires. */
|
|
39
|
+
expires_in: number;
|
|
40
|
+
file_id?: string;
|
|
41
|
+
filename?: string;
|
|
42
|
+
}
|
|
43
|
+
/** Ack from job creation — no transcript yet; poll getJob or waitForJob. */
|
|
44
|
+
interface CreateJobResponse {
|
|
45
|
+
id: string;
|
|
46
|
+
status: PublicJobStatus;
|
|
47
|
+
model?: string | null;
|
|
48
|
+
service_mode?: ServiceMode | null;
|
|
49
|
+
language?: string | null;
|
|
50
|
+
created_at?: string | null;
|
|
51
|
+
}
|
|
52
|
+
interface CancelJobResponse {
|
|
53
|
+
success: boolean;
|
|
54
|
+
status: string;
|
|
55
|
+
}
|
|
56
|
+
interface ListJobsResponse {
|
|
57
|
+
data?: V1Job[];
|
|
58
|
+
pagination?: {
|
|
59
|
+
page?: number;
|
|
60
|
+
limit?: number;
|
|
61
|
+
total?: number;
|
|
62
|
+
[key: string]: unknown;
|
|
63
|
+
};
|
|
64
|
+
[key: string]: unknown;
|
|
65
|
+
}
|
|
66
|
+
interface SpeechWeaveClientOptions {
|
|
67
|
+
/** Falls back to SPEECHWEAVE_API_KEY. Required if the env var is unset. */
|
|
68
|
+
api_key?: string;
|
|
69
|
+
/** Defaults to https://api.speechweave.com/v1. */
|
|
70
|
+
base_url?: string;
|
|
71
|
+
/** Override global fetch (tests, custom agents). */
|
|
72
|
+
fetch_func?: typeof fetch;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Low-level SpeechWeave `/v1` client (presign, jobs, uploads). */
|
|
76
|
+
declare class SpeechWeaveClient {
|
|
77
|
+
protected readonly api_key: string;
|
|
78
|
+
protected readonly base_url: string;
|
|
79
|
+
protected readonly fetch_func: typeof fetch;
|
|
80
|
+
/**
|
|
81
|
+
* @param options.api_key - Falls back to SPEECHWEAVE_API_KEY. Throws if neither is set.
|
|
82
|
+
* @param options.base_url - Defaults to https://api.speechweave.com/v1.
|
|
83
|
+
*/
|
|
84
|
+
constructor(options?: SpeechWeaveClientOptions);
|
|
85
|
+
/**
|
|
86
|
+
* Authenticated fetch against base_url.
|
|
87
|
+
* Adds Bearer and User-Agent when missing. Path is relative (leading `/` optional).
|
|
88
|
+
*/
|
|
89
|
+
rawFetch(path: string, init?: RequestInit): Promise<Response>;
|
|
90
|
+
protected requestJson<T>(method: string, path: string, json?: unknown, params?: Record<string, string | number | boolean | undefined | null>): Promise<T>;
|
|
91
|
+
/**
|
|
92
|
+
* Request a short-lived PUT URL and object_key for direct upload to storage.
|
|
93
|
+
*
|
|
94
|
+
* @param params.filename - Original name (used in the storage key).
|
|
95
|
+
* @param params.content_type - MIME type that must match the subsequent PUT.
|
|
96
|
+
*/
|
|
97
|
+
presignUpload(params: {
|
|
98
|
+
filename: string;
|
|
99
|
+
content_type: string;
|
|
100
|
+
}): Promise<PresignResponse>;
|
|
101
|
+
/**
|
|
102
|
+
* PUT audio bytes to a presigned upload_url.
|
|
103
|
+
* Sets Content-Length when the body can be measured; pass file_size for pipes/live streams.
|
|
104
|
+
*
|
|
105
|
+
* @param uploadUrl - upload_url from presignUpload.
|
|
106
|
+
*/
|
|
107
|
+
putToPresignedUrl(uploadUrl: string, body: Buffer | ReadStream | Blob, contentType: string, options?: {
|
|
108
|
+
file_size?: number;
|
|
109
|
+
}): Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Presign → PUT → create job. Returns the create ack (no transcript); poll getJob or waitForJob.
|
|
112
|
+
* Omitting service_mode leaves the API default (deferred). Synchronous rejects files over the sync size cap (default 512 MiB).
|
|
113
|
+
*
|
|
114
|
+
* @param options.filename - Defaults to audio.bin.
|
|
115
|
+
* @param options.content_type - Defaults to application/octet-stream.
|
|
116
|
+
* @param options.language - Two-letter ISO code (e.g. 'en', 'es').
|
|
117
|
+
* @param options.file_size - Content-Length when the body cannot be measured (pipes, live streams).
|
|
118
|
+
*/
|
|
119
|
+
transcribeFile(file: ReadStream | Buffer | Blob, options?: {
|
|
120
|
+
filename?: string;
|
|
121
|
+
content_type?: string;
|
|
122
|
+
model?: string;
|
|
123
|
+
service_mode?: ServiceMode;
|
|
124
|
+
language?: string;
|
|
125
|
+
metadata?: Record<string, unknown>;
|
|
126
|
+
file_size?: number;
|
|
127
|
+
}): Promise<CreateJobResponse>;
|
|
128
|
+
/**
|
|
129
|
+
* Same as {@link transcribeFile}, then poll until completed, failed, or cancelled.
|
|
130
|
+
*
|
|
131
|
+
* @param options.wait_timeout_ms - Defaults to SPEECHWEAVE_JOB_WAIT_MS or 300_000.
|
|
132
|
+
* @param options.poll_ms - Poll interval; defaults to 1500.
|
|
133
|
+
*/
|
|
134
|
+
transcribeFileBlocking(file: ReadStream | Buffer | Blob, options?: {
|
|
135
|
+
filename?: string;
|
|
136
|
+
content_type?: string;
|
|
137
|
+
model?: string;
|
|
138
|
+
service_mode?: ServiceMode;
|
|
139
|
+
language?: string;
|
|
140
|
+
metadata?: Record<string, unknown>;
|
|
141
|
+
file_size?: number;
|
|
142
|
+
wait_timeout_ms?: number;
|
|
143
|
+
poll_ms?: number;
|
|
144
|
+
}): Promise<V1Job>;
|
|
145
|
+
/**
|
|
146
|
+
* Create a transcription job from an already-uploaded object or a remote URL.
|
|
147
|
+
* Provide exactly one of object_key, input_url, or audio_url. type defaults to transcription.
|
|
148
|
+
* Omitting service_mode leaves the API default (deferred).
|
|
149
|
+
*
|
|
150
|
+
* @param params.object_key - From PresignResponse after a successful PUT.
|
|
151
|
+
* @param params.input_url - Publicly reachable audio URL.
|
|
152
|
+
* @param params.audio_url - Alias for input_url.
|
|
153
|
+
* @param params.language - Two-letter ISO code (e.g. 'en', 'es').
|
|
154
|
+
*/
|
|
155
|
+
createJob(params: {
|
|
156
|
+
object_key?: string;
|
|
157
|
+
input_url?: string;
|
|
158
|
+
audio_url?: string;
|
|
159
|
+
model?: string;
|
|
160
|
+
service_mode?: ServiceMode;
|
|
161
|
+
language?: string;
|
|
162
|
+
type?: string;
|
|
163
|
+
metadata?: Record<string, unknown>;
|
|
164
|
+
}): Promise<CreateJobResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* Fetch the current job record (status, transcript when completed, error when failed).
|
|
167
|
+
*
|
|
168
|
+
* @param job_id - Id from createJob / transcribeFile.
|
|
169
|
+
*/
|
|
170
|
+
getJob(job_id: string): Promise<V1Job>;
|
|
171
|
+
/**
|
|
172
|
+
* List jobs for the authenticated account.
|
|
173
|
+
*
|
|
174
|
+
* @param params.status - Filter by PublicJobStatus value.
|
|
175
|
+
* @param params.page - 1-based page; API default if omitted.
|
|
176
|
+
* @param params.limit - Page size; API default if omitted.
|
|
177
|
+
*/
|
|
178
|
+
listJobs(params?: {
|
|
179
|
+
page?: number;
|
|
180
|
+
limit?: number;
|
|
181
|
+
status?: string;
|
|
182
|
+
}): Promise<ListJobsResponse>;
|
|
183
|
+
/**
|
|
184
|
+
* Cancel a pending or processing job.
|
|
185
|
+
* Fails if the job is already completed, failed, or cancelled.
|
|
186
|
+
*
|
|
187
|
+
* @param job_id - Id from createJob / transcribeFile.
|
|
188
|
+
*/
|
|
189
|
+
cancelJob(job_id: string): Promise<CancelJobResponse>;
|
|
190
|
+
/**
|
|
191
|
+
* Open a disk path and run {@link transcribeFile}.
|
|
192
|
+
* filename is taken from the path basename.
|
|
193
|
+
*
|
|
194
|
+
* @param file_path - Absolute or relative path on the local filesystem.
|
|
195
|
+
*/
|
|
196
|
+
transcribeFilePath(file_path: string, options?: {
|
|
197
|
+
model?: string;
|
|
198
|
+
service_mode?: ServiceMode;
|
|
199
|
+
language?: string;
|
|
200
|
+
content_type?: string;
|
|
201
|
+
metadata?: Record<string, unknown>;
|
|
202
|
+
file_size?: number;
|
|
203
|
+
}): Promise<CreateJobResponse>;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
declare class SpeechWeaveError extends Error {
|
|
207
|
+
readonly status: number;
|
|
208
|
+
readonly code?: string;
|
|
209
|
+
readonly body?: unknown;
|
|
210
|
+
readonly retry_after?: number;
|
|
211
|
+
constructor(message: string, status?: number, code?: string, body?: unknown, retry_after?: number);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
interface WaitForJobOptions {
|
|
215
|
+
/** Max wait in ms; defaults to 3_600_000. */
|
|
216
|
+
timeout_ms?: number;
|
|
217
|
+
/** Interval between getJob polls; defaults to 2000. */
|
|
218
|
+
poll_ms?: number;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Poll getJob until completed, failed, or cancelled.
|
|
222
|
+
* Throws SpeechWeaveError with code JOB_WAIT_TIMEOUT on deadline.
|
|
223
|
+
*
|
|
224
|
+
* @param job_id - Id from createJob / transcribeFile.
|
|
225
|
+
*/
|
|
226
|
+
declare function waitForJob(client: SpeechWeaveClient, job_id: string, opts?: WaitForJobOptions): Promise<V1Job>;
|
|
227
|
+
|
|
228
|
+
type UploadBody = Buffer | Blob | ReadStream | Uint8Array;
|
|
229
|
+
declare function shapeOpenAiResponse(job: Record<string, unknown>): {
|
|
230
|
+
text: string;
|
|
231
|
+
task: "transcribe";
|
|
232
|
+
duration?: number;
|
|
233
|
+
language?: string;
|
|
234
|
+
};
|
|
235
|
+
declare function shapeDeepgramResponse(job: Record<string, unknown>, options?: {
|
|
236
|
+
model?: string | null;
|
|
237
|
+
}): Record<string, unknown>;
|
|
238
|
+
declare function shapeAssemblyResponse(job: Record<string, unknown>): Record<string, unknown>;
|
|
239
|
+
declare function uploadAndCreateJob(client: SpeechWeaveClient, params: {
|
|
240
|
+
data: UploadBody;
|
|
241
|
+
filename: string;
|
|
242
|
+
content_type?: string;
|
|
243
|
+
model?: string;
|
|
244
|
+
language?: string;
|
|
245
|
+
service_mode?: ServiceMode;
|
|
246
|
+
metadata?: Record<string, unknown>;
|
|
247
|
+
file_size?: number;
|
|
248
|
+
}): Promise<CreateJobResponse>;
|
|
249
|
+
declare function createJobFromUrl(client: SpeechWeaveClient, params: {
|
|
250
|
+
url: string;
|
|
251
|
+
model?: string;
|
|
252
|
+
language?: string;
|
|
253
|
+
service_mode?: ServiceMode;
|
|
254
|
+
metadata?: Record<string, unknown>;
|
|
255
|
+
}): Promise<CreateJobResponse>;
|
|
256
|
+
declare function finishCompatJob(client: SpeechWeaveClient, job: CreateJobResponse | V1Job | Record<string, unknown>, options: {
|
|
257
|
+
wait: boolean;
|
|
258
|
+
error_code: string;
|
|
259
|
+
timeout_ms?: number;
|
|
260
|
+
poll_ms?: number;
|
|
261
|
+
}): Promise<V1Job | CreateJobResponse | Record<string, unknown>>;
|
|
262
|
+
|
|
263
|
+
declare const VERSION = "1.0.0";
|
|
264
|
+
|
|
265
|
+
interface VerifyWebhookResult {
|
|
266
|
+
ok: boolean;
|
|
267
|
+
reason?: string;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Verify `SpeechWeave-Signature` header (Stripe-style: `t=unix,v1=hex` — may include multiple `v1=` during secret rotation).
|
|
271
|
+
*
|
|
272
|
+
* @param secret — active webhook signing secret, or array of `[active, previous]` during a rotation window
|
|
273
|
+
* @param raw_body — exact request body string (use raw bytes as UTF-8 string)
|
|
274
|
+
* @param signature_header — value of the `SpeechWeave-Signature` header
|
|
275
|
+
* @param tolerance_seconds — max clock skew; defaults to 300
|
|
276
|
+
*/
|
|
277
|
+
declare function verifyWebhook(secret: string | string[], raw_body: string, signature_header: string, tolerance_seconds?: number): VerifyWebhookResult;
|
|
278
|
+
|
|
279
|
+
interface OpenAiTranscriptionCreateOptions {
|
|
280
|
+
file: UploadBody;
|
|
281
|
+
filename?: string;
|
|
282
|
+
model?: string;
|
|
283
|
+
language?: string;
|
|
284
|
+
file_size?: number;
|
|
285
|
+
wait?: boolean;
|
|
286
|
+
}
|
|
287
|
+
declare function createOpenAiAudioNamespace(client: SpeechWeaveClient): {
|
|
288
|
+
transcriptions: {
|
|
289
|
+
/** OpenAI-shaped transcription create — drop-in compatibility wrapper. */
|
|
290
|
+
create: (opts: OpenAiTranscriptionCreateOptions) => Promise<Record<string, unknown> | CreateJobResponse | {
|
|
291
|
+
text: string;
|
|
292
|
+
task: "transcribe";
|
|
293
|
+
duration?: number;
|
|
294
|
+
language?: string;
|
|
295
|
+
}>;
|
|
296
|
+
};
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
interface DeepgramTranscribeFileOptions {
|
|
300
|
+
model?: string;
|
|
301
|
+
filename?: string;
|
|
302
|
+
language?: string;
|
|
303
|
+
file_size?: number;
|
|
304
|
+
wait?: boolean;
|
|
305
|
+
}
|
|
306
|
+
interface DeepgramTranscribeUrlOptions {
|
|
307
|
+
model?: string;
|
|
308
|
+
language?: string;
|
|
309
|
+
wait?: boolean;
|
|
310
|
+
}
|
|
311
|
+
declare function createDeepgramListenNamespace(client: SpeechWeaveClient): {
|
|
312
|
+
prerecorded: {
|
|
313
|
+
/** Deepgram-shaped prerecorded file transcription — drop-in compatibility wrapper. */
|
|
314
|
+
transcribeFile: (stream: UploadBody | ReadStream, options?: DeepgramTranscribeFileOptions) => Promise<Record<string, unknown> | CreateJobResponse>;
|
|
315
|
+
/** Deepgram-shaped prerecorded URL transcription — drop-in compatibility wrapper. */
|
|
316
|
+
transcribeUrl: (url: string, options?: DeepgramTranscribeUrlOptions) => Promise<Record<string, unknown> | CreateJobResponse>;
|
|
317
|
+
};
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
interface AssemblyTranscribeConfig {
|
|
321
|
+
model?: string;
|
|
322
|
+
language?: string;
|
|
323
|
+
file_size?: number;
|
|
324
|
+
}
|
|
325
|
+
declare function createAssemblyTranscriptsNamespace(client: SpeechWeaveClient): {
|
|
326
|
+
/** AssemblyAI-shaped transcription — drop-in compatibility wrapper. Pass a URL string or binary/file body. */
|
|
327
|
+
transcribe: (audio: string | UploadBody | ReadStream, config?: AssemblyTranscribeConfig, options?: {
|
|
328
|
+
wait?: boolean;
|
|
329
|
+
}) => Promise<Record<string, unknown> | CreateJobResponse>;
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
declare function createJobsNamespace(client: SpeechWeaveClient): {
|
|
333
|
+
/**
|
|
334
|
+
* Create a job from a local file or a remote URL / object_key.
|
|
335
|
+
* Pass `file` (path, Buffer, Blob, or stream) to presign-upload then create.
|
|
336
|
+
* Otherwise pass one of `object_key`, `input_url`, or `audio_url` (no upload).
|
|
337
|
+
* Omitting service_mode leaves the API default (deferred).
|
|
338
|
+
*/
|
|
339
|
+
create: (params: {
|
|
340
|
+
file?: string | Buffer | Blob | node_fs.ReadStream;
|
|
341
|
+
filename?: string;
|
|
342
|
+
content_type?: string;
|
|
343
|
+
object_key?: string;
|
|
344
|
+
input_url?: string;
|
|
345
|
+
audio_url?: string;
|
|
346
|
+
model?: string;
|
|
347
|
+
service_mode?: ServiceMode;
|
|
348
|
+
language?: string;
|
|
349
|
+
type?: string;
|
|
350
|
+
metadata?: Record<string, unknown>;
|
|
351
|
+
file_size?: number;
|
|
352
|
+
}) => Promise<CreateJobResponse>;
|
|
353
|
+
/** Fetch the current job record. */
|
|
354
|
+
get: (job_id: string) => Promise<V1Job>;
|
|
355
|
+
/** List jobs for the authenticated account. */
|
|
356
|
+
list: (params?: {
|
|
357
|
+
page?: number;
|
|
358
|
+
limit?: number;
|
|
359
|
+
status?: string;
|
|
360
|
+
}) => Promise<ListJobsResponse>;
|
|
361
|
+
/**
|
|
362
|
+
* Cancel a pending or processing job.
|
|
363
|
+
* Fails if the job is already completed, failed, or cancelled.
|
|
364
|
+
*/
|
|
365
|
+
cancel: (job_id: string) => Promise<CancelJobResponse>;
|
|
366
|
+
};
|
|
367
|
+
declare class SpeechWeave extends SpeechWeaveClient {
|
|
368
|
+
readonly audio: ReturnType<typeof createOpenAiAudioNamespace>;
|
|
369
|
+
readonly listen: ReturnType<typeof createDeepgramListenNamespace>;
|
|
370
|
+
readonly transcripts: ReturnType<typeof createAssemblyTranscriptsNamespace>;
|
|
371
|
+
readonly jobs: ReturnType<typeof createJobsNamespace>;
|
|
372
|
+
constructor(options?: SpeechWeaveClientOptions);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export { type CreateJobResponse, type ListJobsResponse, type PresignResponse, type PublicJobStatus, type ServiceMode, SpeechWeave, SpeechWeaveClient, type SpeechWeaveClientOptions, SpeechWeaveError, type V1Job, VERSION, type VerifyWebhookResult, type WaitForJobOptions, createJobFromUrl, finishCompatJob, shapeAssemblyResponse, shapeDeepgramResponse, shapeOpenAiResponse, uploadAndCreateJob, verifyWebhook, waitForJob };
|