Skip to content

Commit b6669ea

Browse files
committed
chore: mergy merge
2 parents 54c9085 + 630387d commit b6669ea

19 files changed

+347
-14
lines changed

package-lock.json

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@flydotio/dockerfile": "^0.4.10",
6565
"@types/chai": "^4.3.5",
6666
"@types/mocha": "^9.1.1",
67+
"@types/node": "^18.19.39",
6768
"@types/sinon": "^17.0.3",
6869
"@types/ws": "^8.5.10",
6970
"chai": "^4.3.7",

src/DeepgramClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
OnPremClient,
88
SelfHostedRestClient,
99
SpeakClient,
10+
ModelsRestClient,
1011
} from "./packages";
1112

1213
/**
@@ -33,6 +34,15 @@ export default class DeepgramClient extends AbstractClient {
3334
return new ManageClient(this.options);
3435
}
3536

37+
/**
38+
* Returns a new instance of the ModelsRestClient, which provides access to the Deepgram API's model functionality.
39+
*
40+
* @returns {ModelsRestClient} A new instance of the ModelsRestClient.
41+
*/
42+
get models(): ModelsRestClient {
43+
return new ModelsRestClient(this.options);
44+
}
45+
3646
/**
3747
* Returns a new instance of the SelfHostedRestClient, which provides access to the Deepgram API's self-hosted functionality.
3848
*

src/lib/constants.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { convertProtocolToWs, isBrowser } from "./helpers";
1+
import { convertProtocolToWs, isBrowser, isBun, isNode } from "./helpers";
22
import { version } from "./version";
33
import type { DefaultNamespaceOptions, DefaultClientOptions } from "./types";
44

@@ -7,17 +7,32 @@ export const NODE_VERSION =
77
? process.versions.node
88
: "unknown";
99

10+
export const BUN_VERSION =
11+
typeof process !== "undefined" && process.versions && process.versions.bun
12+
? process.versions.bun
13+
: "unknown";
14+
1015
export const BROWSER_AGENT =
1116
typeof window !== "undefined" && window.navigator && window.navigator.userAgent
1217
? window.navigator.userAgent
1318
: "unknown";
1419

20+
const getAgent = () => {
21+
if (isNode()) {
22+
return `node/${NODE_VERSION}`;
23+
} else if (isBun()) {
24+
return `bun/${BUN_VERSION}`;
25+
} else if (isBrowser()) {
26+
return `javascript ${BROWSER_AGENT}`;
27+
} else {
28+
return `unknown`;
29+
}
30+
};
31+
1532
export const DEFAULT_HEADERS = {
1633
"Content-Type": `application/json`,
1734
"X-Client-Info": `@deepgram/sdk; ${isBrowser() ? "browser" : "server"}; v${version}`,
18-
"User-Agent": `@deepgram/sdk/${version} ${
19-
isBrowser() ? `javascript ${BROWSER_AGENT}` : `node/${NODE_VERSION}`
20-
}`,
35+
"User-Agent": `@deepgram/sdk/${version} ${getAgent()}`,
2136
};
2237

2338
export const DEFAULT_URL = "https://api.deepgram.com";

src/lib/helpers.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import {
1111
import { Headers as CrossFetchHeaders } from "cross-fetch";
1212
import { Readable } from "stream";
1313
import merge from "deepmerge";
14+
import { BROWSER_AGENT, BUN_VERSION, NODE_VERSION } from "./constants";
1415

1516
export function stripTrailingSlash(url: string): string {
1617
return url.replace(/\/$/, "");
1718
}
1819

19-
export function isBrowser() {
20-
return typeof window !== "undefined" && typeof window.document !== "undefined";
21-
}
20+
export const isBrowser = () => BROWSER_AGENT !== "unknown";
21+
22+
export const isNode = () => NODE_VERSION !== "unknown";
23+
24+
export const isBun = () => BUN_VERSION !== "unknown";
2225

2326
export function applyDefaults<O, S>(options: Partial<O> = {}, subordinate: Partial<S> = {}): S {
2427
return merge(subordinate, options);

src/lib/types/GetModelsResponse.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
type Model = {
2+
name: string;
3+
canonical_name: string;
4+
architecture: string;
5+
languages?: string[];
6+
version: string;
7+
uuid: string;
8+
batch?: boolean;
9+
streaming?: boolean;
10+
formatted_output?: boolean;
11+
metadata?: {
12+
accent: string;
13+
color: string;
14+
image: string;
15+
sample: string;
16+
};
17+
};
18+
19+
export type GetModelResponse = Model;
20+
21+
export type GetModelsResponse = {
22+
stt: Model[];
23+
tts: Model[];
24+
};

src/lib/types/GetModelsSchema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface GetModelsSchema extends Record<string, unknown> {
2+
include_outdated?: boolean;
3+
}

src/lib/types/LiveTranscriptionEvent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ export interface LiveTranscriptionEvent {
99
alternatives: {
1010
transcript: string;
1111
confidence: number;
12+
languages: string[];
1213
words: {
1314
word: string;
1415
start: number;
1516
end: number;
1617
confidence: number;
18+
language: string;
1719
punctuated_word: string;
1820
}[];
1921
}[];

src/lib/types/SyncPrerecordedResponse.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface Alternative {
1212
entities?: Entity[];
1313
translations?: Translation[];
1414
topics?: TopicGroup[];
15+
languages?: string[];
1516
}
1617

1718
interface Channel {
@@ -208,4 +209,5 @@ interface WordBase {
208209
punctuated_word?: string;
209210
speaker?: number;
210211
speaker_confidence?: number;
212+
language?: string;
211213
}

src/lib/types/TranscriptionSchema.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ interface PrerecordedSchema extends TranscriptionSchema {
149149
/**
150150
* @see https://developers.deepgram.com/docs/language-detection
151151
*/
152-
detect_language?: boolean;
152+
detect_language?: boolean | string[];
153153

154154
/**
155155
* @see https://developers.deepgram.com/docs/topic-detection
@@ -222,6 +222,11 @@ interface LiveSchema extends TranscriptionSchema {
222222
*/
223223
interim_results?: boolean;
224224

225+
/**
226+
* @see https://developers.deepgram.com/docs/smart-format#using-no-delay
227+
*/
228+
no_delay?: boolean;
229+
225230
/**
226231
* @see https://developers.deepgram.com/docs/understanding-end-of-speech-detection
227232
*/

src/lib/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export * from "./DeepgramClientOptions";
88
export * from "./DeepgramResponse";
99
export * from "./DeepgramSource";
1010
export * from "./Fetch";
11+
export * from "./GetModelsResponse";
12+
export * from "./GetModelsSchema";
1113
export * from "./GetProjectBalancesResponse";
1214
export * from "./GetProjectInvitesResponse";
1315
export * from "./GetProjectKeysResponse";

src/packages/AbstractClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import EventEmitter from "events";
1+
import { EventEmitter } from "events";
22
import { DEFAULT_OPTIONS, DEFAULT_URL } from "../lib/constants";
33
import { DeepgramError } from "../lib/errors";
44
import { appendSearchParams, applyDefaults, convertLegacyOptions } from "../lib/helpers";

src/packages/AbstractLiveClient.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AbstractClient, noop } from "./AbstractClient";
22
import { CONNECTION_STATE, SOCKET_STATES } from "../lib/constants";
33
import type { DeepgramClientOptions, LiveSchema } from "../lib/types";
44
import type { WebSocket as WSWebSocket } from "ws";
5+
import { isBun } from "../lib/helpers";
56

67
/**
78
* Represents a constructor for a WebSocket-like object that can be used in the application.
@@ -119,6 +120,23 @@ export abstract class AbstractLiveClient extends AbstractClient {
119120
return;
120121
}
121122

123+
/**
124+
* @summary Bun websocket transport has a bug where it's native WebSocket implementation messes up the headers
125+
* @summary This is a workaround to use the WS package for the websocket connection instead of the native Bun WebSocket
126+
* @summary you can track the issue here
127+
* @link https://github.com/oven-sh/bun/issues/4529
128+
*/
129+
if (isBun()) {
130+
import("ws").then(({ default: WS }) => {
131+
this.conn = new WS(requestUrl, {
132+
headers: this.headers,
133+
});
134+
console.log(`Using WS package`);
135+
this.setupConnection();
136+
});
137+
return;
138+
}
139+
122140
/**
123141
* Native websocket transport (browser)
124142
*/

src/packages/ListenLiveClient.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,18 @@ export class ListenLiveClient extends AbstractLiveClient {
112112
}
113113

114114
/**
115-
* @deprecated Since version 3.4. Will be removed in version 4.0. Use `close` instead.
115+
* Sends a "Finalize" message to flush any transcription sitting in the server's buffer.
116+
*/
117+
public finalize(): void {
118+
this.send(
119+
JSON.stringify({
120+
type: "Finalize",
121+
})
122+
);
123+
}
124+
125+
/**
126+
* @deprecated Since version 3.4. Will be removed in version 4.0. Use `requestClose` instead.
116127
*/
117128
public finish(): void {
118129
this.requestClose();

src/packages/ManageRestClient.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import type {
2525
UpdateProjectSchema,
2626
VoidResponse,
2727
GetTokenDetailsResponse,
28+
GetModelsResponse,
29+
GetModelResponse,
30+
GetModelsSchema,
2831
} from "../lib/types";
2932
import { AbstractRestClient } from "./AbstractRestClient";
3033

@@ -755,6 +758,85 @@ export class ManageRestClient extends AbstractRestClient {
755758
throw error;
756759
}
757760
}
761+
762+
/**
763+
* Retrieves all models for a given project.
764+
*
765+
* @param projectId - The ID of the project.
766+
* @param endpoint - (optional) The endpoint URL for retrieving models. Defaults to ":version/projects/:projectId/models".
767+
* @returns A promise that resolves to a DeepgramResponse containing the GetModelsResponse.
768+
* @example
769+
* ```typescript
770+
* import { createClient } from "@deepgram/sdk";
771+
*
772+
* const deepgram = createClient(DEEPGRAM_API_KEY);
773+
* const { result: models, error } = deepgram.manage.getAllModels("projectId");
774+
*
775+
* if (error) {
776+
* console.error(error);
777+
* } else {
778+
* console.log(models);
779+
* }
780+
* ```
781+
*/
782+
async getAllModels(
783+
projectId: string,
784+
options: GetModelsSchema = {},
785+
endpoint = ":version/projects/:projectId/models"
786+
): Promise<DeepgramResponse<GetModelsResponse>> {
787+
try {
788+
const requestUrl = this.getRequestUrl(endpoint, { projectId }, options);
789+
const result: GetModelsResponse = await this.get(requestUrl).then((result) => result.json());
790+
791+
return { result, error: null };
792+
} catch (error) {
793+
if (isDeepgramError(error)) {
794+
return { result: null, error };
795+
}
796+
797+
throw error;
798+
}
799+
}
800+
801+
/**
802+
* Retrieves a model from the specified project.
803+
*
804+
* @param projectId - The ID of the project.
805+
* @param modelId - The ID of the model.
806+
* @param endpoint - (optional) The endpoint URL for the request. Default value is ":version/projects/:projectId/models/:modelId".
807+
* @returns A promise that resolves to a DeepgramResponse containing the GetModelResponse.
808+
* @example
809+
* ```typescript
810+
* import { createClient } from "@deepgram/sdk";
811+
*
812+
* const deepgram = createClient(DEEPGRAM_API_KEY);
813+
* const { result: model, error } = deepgram.models.getModel("projectId", "modelId");
814+
*
815+
* if (error) {
816+
* console.error(error);
817+
* } else {
818+
* console.log(model);
819+
* }
820+
* ```
821+
*/
822+
async getModel(
823+
projectId: string,
824+
modelId: string,
825+
endpoint = ":version/projects/:projectId/models/:modelId"
826+
): Promise<DeepgramResponse<GetModelResponse>> {
827+
try {
828+
const requestUrl = this.getRequestUrl(endpoint, { projectId, modelId });
829+
const result: GetModelResponse = await this.get(requestUrl).then((result) => result.json());
830+
831+
return { result, error: null };
832+
} catch (error) {
833+
if (isDeepgramError(error)) {
834+
return { result: null, error };
835+
}
836+
837+
throw error;
838+
}
839+
}
758840
}
759841

760842
export { ManageRestClient as ManageClient };

0 commit comments

Comments
 (0)