Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

January minor release #238

Merged
merged 8 commits into from
Jan 25, 2024
4 changes: 4 additions & 0 deletions examples/node-live/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const live = async () => {
console.log(data);
});

connection.on(LiveTranscriptionEvents.Error, (err) => {
console.error(err);
});

fetch(url)
.then((r) => r.body)
.then((res) => {
Expand Down
37 changes: 37 additions & 0 deletions src/DeepgramClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DeepgramVersionError } from "./lib/errors";
import { AbstractClient } from "./packages/AbstractClient";
import { ListenClient } from "./packages/ListenClient";
import { ManageClient } from "./packages/ManageClient";
Expand All @@ -21,4 +22,40 @@ export default class DeepgramClient extends AbstractClient {
get onprem(): OnPremClient {
return new OnPremClient(this.key, this.options);
}

/**
* Major version fallback errors are below
*/

get transcription(): any {
throw new DeepgramVersionError();
}

get projects(): any {
throw new DeepgramVersionError();
}

get keys(): any {
throw new DeepgramVersionError();
}

get members(): any {
throw new DeepgramVersionError();
}

get scopes(): any {
throw new DeepgramVersionError();
}

get invitation(): any {
throw new DeepgramVersionError();
}

get usage(): any {
throw new DeepgramVersionError();
}

get billing(): any {
throw new DeepgramVersionError();
}
}
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import DeepgramClient from "./DeepgramClient";
import { DeepgramVersionError } from "./lib/errors";
import type { DeepgramClientOptions } from "./lib/types";

/**
* Major version fallback error
*/
class Deepgram {
constructor(protected apiKey: string, protected apiUrl?: string, protected requireSSL?: boolean) {
throw new DeepgramVersionError();
}
}

/**
* Creates a new Deepgram Client.
*/
const createClient = (apiKey: string, options: DeepgramClientOptions = {}): DeepgramClient => {
return new DeepgramClient(apiKey, options);
};

export { createClient, DeepgramClient };
export { createClient, DeepgramClient, Deepgram };

/**
* Helpful exports.
Expand Down
2 changes: 2 additions & 0 deletions src/lib/enums/LiveTranscriptionEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export enum LiveTranscriptionEvents {
Metadata = "Metadata", // exact match to data type from API
Error = "error",
Warning = "warning",
UtteranceEnd = "UtteranceEnd", // exact match to data type from API
SpeechStarted = "SpeechStarted",
}
10 changes: 10 additions & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ export class DeepgramUnknownError extends DeepgramError {
this.originalError = originalError;
}
}

export class DeepgramVersionError extends DeepgramError {
constructor() {
super(
`You are attempting to use an old format for a newer SDK version. Read more here: https://dpgr.am/js-v3`
);

this.name = "DeepgramVersionError";
}
}
21 changes: 19 additions & 2 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Headers as CrossFetchHeaders } from "cross-fetch";
import { DeepgramClientOptions, FileSource, PrerecordedSource, UrlSource } from "./types";
import {
DeepgramClientOptions,
FileSource,
PrerecordedSource,
UrlSource,
TextSource,
AnalyzeSource,
} from "./types";
import { Readable } from "stream";
import merge from "deepmerge";

Expand Down Expand Up @@ -41,12 +48,22 @@ export const resolveHeadersConstructor = () => {
return Headers;
};

export const isUrlSource = (providedSource: PrerecordedSource): providedSource is UrlSource => {
export const isUrlSource = (
providedSource: PrerecordedSource | AnalyzeSource
): providedSource is UrlSource => {
if ((providedSource as UrlSource).url) return true;

return false;
};

export const isTextSource = (
providedSource: PrerecordedSource | AnalyzeSource
): providedSource is TextSource => {
if ((providedSource as TextSource).text) return true;

return false;
};

export const isFileSource = (providedSource: PrerecordedSource): providedSource is FileSource => {
if (isReadStreamSource(providedSource) || isBufferSource(providedSource)) return true;

Expand Down
28 changes: 28 additions & 0 deletions src/lib/types/AnalyzeSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Options for read analysis
*/
interface AnalyzeSchema extends Record<string, unknown> {
callback?: string;

callback_method?: string;

custom_intent?: string | string[];

custom_intent_mode?: "strict" | "extended";

custom_topic?: string | string[];

custom_topic_mode?: "strict" | "extended";

intents?: boolean;

language?: string;

summarize?: boolean;

sentiment?: boolean;

topics?: boolean;
}

export type { AnalyzeSchema };
3 changes: 3 additions & 0 deletions src/lib/types/AsyncAnalyzeResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AsyncAnalyzeResponse {
request_id: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ export type FileSource = Buffer | Readable;
export interface UrlSource {
url: string;
}

export interface TextSource {
text: string;
}

export type AnalyzeSource = UrlSource | TextSource;
5 changes: 5 additions & 0 deletions src/lib/types/SpeechStartedEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface SpeechStartedEvent {
type: "SpeechStarted";
channel: number[];
timestamp: number;
}
88 changes: 88 additions & 0 deletions src/lib/types/SyncAnalyzeResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
export interface SyncAnalyzeResponse {
model_uuid: string;
metadata: Metadata;
results: Results;
}

interface IntentsInfo {
model_uuid: string;
input_tokens: number;
output_tokens: number;
}

interface SentimentInfo {
model_uuid: string;
input_tokens: number;
output_tokens: number;
}

interface SummaryInfo {
model_uuid: string;
input_tokens: number;
output_tokens: number;
}

interface TopicsInfo {
model_uuid: string;
input_tokens: number;
output_tokens: number;
}

interface Metadata {
request_id: string;
created: string;
language: string;
intents_info: IntentsInfo;
sentiment_info: SentimentInfo;
summary_info: SummaryInfo;
topics_info: TopicsInfo;
}

interface Average {
sentiment: string;
sentiment_score: number;
}

interface Summary {
text: string;
}

interface Topic {
topic: string;
confidence_score: number;
}

interface Intent {
intent: string;
confidence_score: number;
}

interface Segment {
text: string;
start_word: number;
end_word: number;
sentiment: "positive" | "neutral" | "negative";
sentiment_score?: number;
topics?: Topic[];
intents?: Intent[];
}

interface Sentiments {
segments: Segment[];
average: Average;
}

interface Topics {
segments: Segment[];
}

interface Intents {
segments: Segment[];
}

interface Results {
sentiments?: Sentiments;
summary?: Summary;
topics?: Topics;
intents?: Intents;
}
74 changes: 73 additions & 1 deletion src/lib/types/SyncPrerecordedResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ interface Metadata {
duration: number;
channels: number;
models: string[];
model_info: Record<string, ModelInfo>;
warnings?: Warning[];
model_info: Record<string, ModelInfo>;
summary_info?: SummaryInfo;
intents_info?: IntentsInfo;
sentiment_info?: SentimentInfo;
topics_info?: TopicsInfo;
extra: {
[key: string]: unknown;
};
}

interface ModelInfo {
Expand All @@ -54,6 +61,30 @@ interface ModelInfo {
arch: string;
}

interface SummaryInfo {
input_tokens: number;
output_tokens: number;
model_uuid: string;
}

interface IntentsInfo {
model_uuid: string;
input_tokens: number;
output_tokens: number;
}

interface SentimentInfo {
model_uuid: string;
input_tokens: number;
output_tokens: number;
}

interface TopicsInfo {
model_uuid: string;
input_tokens: number;
output_tokens: number;
}

interface Paragraph {
sentences: Sentence[];
start: number;
Expand All @@ -70,6 +101,47 @@ interface Result {
channels: Channel[];
utterances?: Utterance[];
summary?: TranscriptionSummary;
sentiments?: Sentiments;
topics?: Topics;
intents?: Intents;
}

interface Sentiments {
segments: Segment[];
average: Average;
}

interface Topics {
segments: Segment[];
}

interface Intents {
segments: Segment[];
}

interface Intent {
intent: string;
confidence_score: number;
}

interface Average {
sentiment: string;
sentiment_score: number;
}

interface Topic {
topic: string;
confidence_score: number;
}

interface Segment {
text: string;
start_word: number;
end_word: number;
sentiment?: string;
sentiment_score?: number;
topics?: Topic[];
intents?: Intent[];
}

interface Search {
Expand Down
Loading
Loading