Skip to content

Commit

Permalink
feat: sits for JS sdk (#233)
Browse files Browse the repository at this point in the history
* feat: add audio sits features to JS sdk

* fix: fix typos in sync+callback error

* feat: sits analyze types and schema
  • Loading branch information
lukeocodes authored Jan 25, 2024
1 parent 1ec3603 commit d8de666
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 6 deletions.
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;
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;
}
71 changes: 70 additions & 1 deletion src/lib/types/SyncPrerecordedResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ 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;
}

interface ModelInfo {
Expand All @@ -54,6 +58,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 +98,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
35 changes: 35 additions & 0 deletions src/lib/types/TranscriptionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,41 @@ interface TranscriptionSchema extends Record<string, unknown> {
*/
tag?: string[];

/**
* As yet unreleased.
*/
sentiment?: boolean;

/**
* As yet unreleased.
*/
intents?: boolean;

/**
* As yet unreleased.
*/
custom_intent?: string[] | string;

/**
* As yet unreleased.
*/
custom_intent_mode?: "strict" | "extended";

/**
* As yet unreleased.
*/
topics?: boolean;

/**
* As yet unreleased.
*/
custom_topic?: string[] | string;

/**
* As yet unreleased.
*/
custom_topic_mode?: "strict" | "extended";

[key: string]: unknown;
}

Expand Down
11 changes: 10 additions & 1 deletion src/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type { AnalyzeSchema } from "./AnalyzeSchema";
export type { AsyncPrerecordedResponse } from "./AsyncPrerecordedResponse";
export type { AsyncAnalyzeResponse } from "./AsyncAnalyzeResponse";
export type { CreateOnPremCredentialsSchema } from "./CreateOnPremCredentialsSchema";
export type { CreateProjectKeySchema } from "./CreateProjectKeySchema";
export type { CreateProjectKeyResponse } from "./CreateProjectKeyResponse";
Expand Down Expand Up @@ -32,9 +34,16 @@ export type { LiveConfigOptions } from "./LiveConfigOptions";
export type { LiveMetadataEvent } from "./LiveMetadataEvent";
export type { LiveTranscriptionEvent } from "./LiveTranscriptionEvent";
export type { MessageResponse } from "./MessageResponse";
export type { FileSource, PrerecordedSource, UrlSource } from "./PrerecordedSource";
export type {
FileSource,
PrerecordedSource,
UrlSource,
TextSource,
AnalyzeSource,
} from "./DeepgramSource";
export type { SendProjectInviteSchema } from "./SendProjectInviteSchema";
export type { SyncPrerecordedResponse } from "./SyncPrerecordedResponse";
export type { SyncAnalyzeResponse } from "./SyncAnalyzeResponse";
export type { TranscriptionSchema, PrerecordedSchema, LiveSchema } from "./TranscriptionSchema";
export type { UpdateProjectMemberScopeSchema } from "./UpdateProjectMemberScopeSchema";
export type { UpdateProjectSchema } from "./UpdateProjectSchema";
Expand Down
4 changes: 2 additions & 2 deletions src/packages/PrerecordedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PrerecordedClient extends AbstractRestfulClient {

if (options !== undefined && "callback" in options) {
throw new DeepgramError(
"Callback cannot be provided as an option to a synchronous transcription. Use `asyncPrerecordedUrl` or `asyncPrerecordedFile` instead."
"Callback cannot be provided as an option to a synchronous transcription. Use `transcribeUrlCallback` or `transcribeFileCallback` instead."
);
}

Expand Down Expand Up @@ -65,7 +65,7 @@ export class PrerecordedClient extends AbstractRestfulClient {

if (options !== undefined && "callback" in options) {
throw new DeepgramError(
"Callback cannot be provided as an option to a synchronous transcription. Use `asyncPrerecordedUrl` or `asyncPrerecordedFile` instead."
"Callback cannot be provided as an option to a synchronous transcription. Use `transcribeUrlCallback` or `transcribeFileCallback` instead."
);
}

Expand Down
Loading

0 comments on commit d8de666

Please sign in to comment.