@deepgram/sdk 3.8.1 → 3.9.0-beta.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.
Files changed (89) hide show
  1. package/dist/main/DeepgramClient.d.ts +8 -1
  2. package/dist/main/DeepgramClient.d.ts.map +1 -1
  3. package/dist/main/DeepgramClient.js +9 -0
  4. package/dist/main/DeepgramClient.js.map +1 -1
  5. package/dist/main/lib/constants.d.ts +2 -0
  6. package/dist/main/lib/constants.d.ts.map +1 -1
  7. package/dist/main/lib/constants.js +9 -1
  8. package/dist/main/lib/constants.js.map +1 -1
  9. package/dist/main/lib/enums/AgentEvents.d.ts +28 -0
  10. package/dist/main/lib/enums/AgentEvents.d.ts.map +1 -0
  11. package/dist/main/lib/enums/AgentEvents.js +32 -0
  12. package/dist/main/lib/enums/AgentEvents.js.map +1 -0
  13. package/dist/main/lib/enums/index.d.ts +1 -0
  14. package/dist/main/lib/enums/index.d.ts.map +1 -1
  15. package/dist/main/lib/enums/index.js +1 -0
  16. package/dist/main/lib/enums/index.js.map +1 -1
  17. package/dist/main/lib/types/AgentLiveSchema.d.ts +129 -0
  18. package/dist/main/lib/types/AgentLiveSchema.d.ts.map +1 -0
  19. package/dist/main/lib/types/AgentLiveSchema.js +3 -0
  20. package/dist/main/lib/types/AgentLiveSchema.js.map +1 -0
  21. package/dist/main/lib/types/DeepgramClientOptions.d.ts +1 -0
  22. package/dist/main/lib/types/DeepgramClientOptions.d.ts.map +1 -1
  23. package/dist/main/lib/types/index.d.ts +1 -0
  24. package/dist/main/lib/types/index.d.ts.map +1 -1
  25. package/dist/main/lib/types/index.js +1 -0
  26. package/dist/main/lib/types/index.js.map +1 -1
  27. package/dist/main/lib/version.d.ts +1 -1
  28. package/dist/main/lib/version.js +1 -1
  29. package/dist/main/packages/AbstractLiveClient.js +2 -2
  30. package/dist/main/packages/AbstractLiveClient.js.map +1 -1
  31. package/dist/main/packages/AgentLiveClient.d.ts +78 -0
  32. package/dist/main/packages/AgentLiveClient.d.ts.map +1 -0
  33. package/dist/main/packages/AgentLiveClient.js +153 -0
  34. package/dist/main/packages/AgentLiveClient.js.map +1 -0
  35. package/dist/main/packages/index.d.ts +1 -0
  36. package/dist/main/packages/index.d.ts.map +1 -1
  37. package/dist/main/packages/index.js +1 -0
  38. package/dist/main/packages/index.js.map +1 -1
  39. package/dist/module/DeepgramClient.d.ts +8 -1
  40. package/dist/module/DeepgramClient.d.ts.map +1 -1
  41. package/dist/module/DeepgramClient.js +10 -1
  42. package/dist/module/DeepgramClient.js.map +1 -1
  43. package/dist/module/lib/constants.d.ts +2 -0
  44. package/dist/module/lib/constants.d.ts.map +1 -1
  45. package/dist/module/lib/constants.js +8 -0
  46. package/dist/module/lib/constants.js.map +1 -1
  47. package/dist/module/lib/enums/AgentEvents.d.ts +28 -0
  48. package/dist/module/lib/enums/AgentEvents.d.ts.map +1 -0
  49. package/dist/module/lib/enums/AgentEvents.js +29 -0
  50. package/dist/module/lib/enums/AgentEvents.js.map +1 -0
  51. package/dist/module/lib/enums/index.d.ts +1 -0
  52. package/dist/module/lib/enums/index.d.ts.map +1 -1
  53. package/dist/module/lib/enums/index.js +1 -0
  54. package/dist/module/lib/enums/index.js.map +1 -1
  55. package/dist/module/lib/types/AgentLiveSchema.d.ts +129 -0
  56. package/dist/module/lib/types/AgentLiveSchema.d.ts.map +1 -0
  57. package/dist/module/lib/types/AgentLiveSchema.js +2 -0
  58. package/dist/module/lib/types/AgentLiveSchema.js.map +1 -0
  59. package/dist/module/lib/types/DeepgramClientOptions.d.ts +1 -0
  60. package/dist/module/lib/types/DeepgramClientOptions.d.ts.map +1 -1
  61. package/dist/module/lib/types/index.d.ts +1 -0
  62. package/dist/module/lib/types/index.d.ts.map +1 -1
  63. package/dist/module/lib/types/index.js +1 -0
  64. package/dist/module/lib/types/index.js.map +1 -1
  65. package/dist/module/lib/version.d.ts +1 -1
  66. package/dist/module/lib/version.js +1 -1
  67. package/dist/module/packages/AbstractLiveClient.js +2 -2
  68. package/dist/module/packages/AbstractLiveClient.js.map +1 -1
  69. package/dist/module/packages/AgentLiveClient.d.ts +78 -0
  70. package/dist/module/packages/AgentLiveClient.d.ts.map +1 -0
  71. package/dist/module/packages/AgentLiveClient.js +149 -0
  72. package/dist/module/packages/AgentLiveClient.js.map +1 -0
  73. package/dist/module/packages/index.d.ts +1 -0
  74. package/dist/module/packages/index.d.ts.map +1 -1
  75. package/dist/module/packages/index.js +1 -0
  76. package/dist/module/packages/index.js.map +1 -1
  77. package/dist/umd/deepgram.js +1 -1
  78. package/package.json +1 -1
  79. package/src/DeepgramClient.ts +11 -0
  80. package/src/lib/constants.ts +9 -0
  81. package/src/lib/enums/AgentEvents.ts +28 -0
  82. package/src/lib/enums/index.ts +1 -0
  83. package/src/lib/types/AgentLiveSchema.ts +190 -0
  84. package/src/lib/types/DeepgramClientOptions.ts +1 -0
  85. package/src/lib/types/index.ts +1 -0
  86. package/src/lib/version.ts +1 -1
  87. package/src/packages/AbstractLiveClient.ts +2 -2
  88. package/src/packages/AgentLiveClient.ts +157 -0
  89. package/src/packages/index.ts +1 -0
@@ -0,0 +1,190 @@
1
+ // TODO: We could probably use this elsewhere?
2
+ type AudioFormat =
3
+ | {
4
+ encoding: "linear16";
5
+ container: "wav" | "none";
6
+ sampleRate: 8000 | 16000 | 24000 | 32000 | 48000;
7
+ }
8
+ | {
9
+ encoding: "mulaw";
10
+ container: "wav" | "none";
11
+ sampleRate: 8000 | 16000;
12
+ }
13
+ | {
14
+ encoding: "alaw";
15
+ container: "wav" | "none";
16
+ sampleRate: 8000 | 16000;
17
+ }
18
+ | {
19
+ encoding: "mp3";
20
+ bitrate: 32000 | 48000;
21
+ }
22
+ | {
23
+ encoding: "opus";
24
+ container: "ogg";
25
+ /**
26
+ * Must be between 4000 and 650000, inclusive.
27
+ */
28
+ bitrate: number;
29
+ }
30
+ | {
31
+ encoding: "flac";
32
+ sampleRate: 8000 | 16000 | 22050 | 32000 | 48000;
33
+ }
34
+ | {
35
+ encoding: "aac";
36
+ /**
37
+ * Must be between 4000 and 192000 inclusive.
38
+ */
39
+ bitrate: number;
40
+ };
41
+
42
+ type ListenModel =
43
+ | "nova-2"
44
+ | "nova-2-meeting"
45
+ | "nova-2-phonecall"
46
+ | "nova-2-voicemail"
47
+ | "nova-2-finance"
48
+ | "nova-2-conversational"
49
+ | "nova-2-video"
50
+ | "nova-2-medical"
51
+ | "nova-2-drivethru"
52
+ | "nova-2-automotive"
53
+ | "nova-2-atc"
54
+ | "nova"
55
+ | "nova-phonecall"
56
+ | "enhanced"
57
+ | "enhanced-meeting"
58
+ | "enhanced-phonecall"
59
+ | "enhanced-finance"
60
+ | "base"
61
+ | "base-meeting"
62
+ | "base-phonecall"
63
+ | "base-voicemail"
64
+ | "base-finance"
65
+ | "base-conversational"
66
+ | "base-video"
67
+ | "whisper-tiny"
68
+ | "whisper"
69
+ | "whisper-small"
70
+ | "whisper-medium"
71
+ | "whisper-large";
72
+
73
+ type SpeakModel =
74
+ | "aura-asteria-en"
75
+ | "aura-luna-en"
76
+ | "aura-stella-en"
77
+ | "aura-athena-en"
78
+ | "aura-hera-en"
79
+ | "aura-orion-en"
80
+ | "aura-arcas-en"
81
+ | "aura-perseus-en"
82
+ | "aura-angus-en"
83
+ | "aura-orpheus-en"
84
+ | "aura-helios-en"
85
+ | "aura-zeus-en";
86
+
87
+ interface ThinkModelFunction {
88
+ name: string;
89
+ description: string;
90
+ url: string;
91
+ headers: [
92
+ {
93
+ key: "authorization";
94
+ value: string;
95
+ }
96
+ ];
97
+ method: "POST";
98
+ parameters: {
99
+ type: string;
100
+ properties: Record<
101
+ string,
102
+ {
103
+ type: string;
104
+ description: string;
105
+ }
106
+ >;
107
+ };
108
+ }
109
+
110
+ type ThinkModel =
111
+ | {
112
+ provider: {
113
+ type: "open_ai";
114
+ };
115
+ model: "gpt-4o-mini";
116
+ instructions: string;
117
+ functions: ThinkModelFunction[];
118
+ }
119
+ | {
120
+ provider: {
121
+ type: "anthropic";
122
+ };
123
+ model: "claude-3-haiku-20240307";
124
+ instructions: string;
125
+ functions: ThinkModelFunction[];
126
+ }
127
+ | {
128
+ provider: {
129
+ type: "groq";
130
+ };
131
+ model: "";
132
+ instructions: string;
133
+ functions: ThinkModelFunction[];
134
+ }
135
+ | {
136
+ provider: {
137
+ type: "custom";
138
+ url: string;
139
+ key: string;
140
+ };
141
+ model: string;
142
+ instructions: string;
143
+ functions: ThinkModelFunction[];
144
+ };
145
+
146
+ /**
147
+ * @see https://developers.deepgram.com/reference/voicebot-api-phase-preview#settingsconfiguration
148
+ */
149
+ interface AgentLiveSchema extends Record<string, unknown> {
150
+ audio: {
151
+ input?: {
152
+ encoding: string;
153
+ sampleRate: number;
154
+ };
155
+ /**
156
+ * @see https://developers.deepgram.com/docs/tts-media-output-settings#audio-format-combinations
157
+ */
158
+ output?: AudioFormat;
159
+ };
160
+ agent: {
161
+ listen: {
162
+ /**
163
+ * @see https://developers.deepgram.com/docs/model
164
+ */
165
+ model: ListenModel;
166
+ };
167
+ speak: {
168
+ /**
169
+ * @see https://developers.deepgram.com/docs/tts-models
170
+ */
171
+ model: SpeakModel;
172
+ };
173
+ /**
174
+ * @see https://developers.deepgram.com/reference/voicebot-api-phase-preview#supported-llm-providers-and-models
175
+ */
176
+ think: ThinkModel;
177
+ };
178
+ context: {
179
+ /**
180
+ * LLM message history (e.g. to restore existing conversation if websocket disconnects)
181
+ */
182
+ messages: [];
183
+ /**
184
+ * Whether to replay the last message, if it is an assistant message.
185
+ */
186
+ replay: boolean;
187
+ };
188
+ }
189
+
190
+ export type { AgentLiveSchema, SpeakModel };
@@ -64,6 +64,7 @@ export interface DeepgramClientOptions {
64
64
  onprem?: NamespaceOptions;
65
65
  read?: NamespaceOptions;
66
66
  speak?: NamespaceOptions;
67
+ agent?: NamespaceOptions;
67
68
 
68
69
  /**
69
70
  * @deprecated as of 3.4, use a namespace like `global` instead
@@ -1,3 +1,4 @@
1
+ export * from "./AgentLiveSchema";
1
2
  export * from "./AnalyzeSchema";
2
3
  export * from "./AsyncAnalyzeResponse";
3
4
  export * from "./AsyncPrerecordedResponse";
@@ -1 +1 @@
1
- export const version = "3.8.1";
1
+ export const version = "3.9.0-beta.1";
@@ -245,8 +245,8 @@ export abstract class AbstractLiveClient extends AbstractClient {
245
245
  }
246
246
 
247
247
  if (typeof data !== "string") {
248
- if (data.byteLength === 0) {
249
- this.log("warn", "skipping `send` for zero-byte blob", data);
248
+ if (!data?.byteLength) {
249
+ this.log("warn", "skipping `send` for zero-byte payload", data);
250
250
 
251
251
  return;
252
252
  }
@@ -0,0 +1,157 @@
1
+ import { DEFAULT_AGENT_URL } from "../lib/constants";
2
+ import { AgentEvents } from "../lib/enums/AgentEvents";
3
+ import type { AgentLiveSchema, SpeakModel, DeepgramClientOptions } from "../lib/types";
4
+ import { AbstractLiveClient } from "./AbstractLiveClient";
5
+
6
+ export class AgentLiveClient extends AbstractLiveClient {
7
+ public namespace: string = "agent";
8
+
9
+ constructor(options: DeepgramClientOptions, endpoint: string = "/agent") {
10
+ super(options);
11
+ this.baseUrl = options.agent?.websocket?.options?.url ?? DEFAULT_AGENT_URL;
12
+
13
+ this.connect({}, endpoint);
14
+ }
15
+
16
+ /**
17
+ * Sets up the connection event handlers.
18
+ * This method is responsible for handling the various events that can occur on the WebSocket connection, such as opening, closing, and receiving messages.
19
+ * - When the connection is opened, it emits the `AgentEvents.Open` event.
20
+ * - When the connection is closed, it emits the `AgentEvents.Close` event.
21
+ * - When an error occurs on the connection, it emits the `AgentEvents.Error` event.
22
+ * - When a message is received, it parses the message and emits the appropriate event based on the message type.
23
+ */
24
+ public setupConnection(): void {
25
+ if (this.conn) {
26
+ this.conn.onopen = () => {
27
+ this.emit(AgentEvents.Open, this);
28
+ };
29
+
30
+ this.conn.onclose = (event: any) => {
31
+ this.emit(AgentEvents.Close, event);
32
+ };
33
+
34
+ this.conn.onerror = (event: ErrorEvent) => {
35
+ this.emit(AgentEvents.Error, event);
36
+ };
37
+
38
+ this.conn.onmessage = (event: MessageEvent) => {
39
+ this.handleMessage(event);
40
+ };
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Handles incoming messages from the WebSocket connection.
46
+ * @param event - The MessageEvent object representing the received message.
47
+ */
48
+ protected handleMessage(event: MessageEvent): void {
49
+ if (typeof event.data === "string") {
50
+ try {
51
+ const data = JSON.parse(event.data);
52
+ this.handleTextMessage(data);
53
+ } catch (error) {
54
+ this.emit(AgentEvents.Error, {
55
+ event,
56
+ message: "Unable to parse `data` as JSON.",
57
+ error,
58
+ });
59
+ }
60
+ } else if (event.data instanceof Blob) {
61
+ event.data.arrayBuffer().then((buffer) => {
62
+ this.handleBinaryMessage(Buffer.from(buffer));
63
+ });
64
+ } else if (event.data instanceof ArrayBuffer) {
65
+ this.handleBinaryMessage(Buffer.from(event.data));
66
+ } else if (Buffer.isBuffer(event.data)) {
67
+ this.handleBinaryMessage(event.data);
68
+ } else {
69
+ console.log("Received unknown data type", event.data);
70
+ this.emit(AgentEvents.Error, {
71
+ event,
72
+ message: "Received unknown data type.",
73
+ });
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Handles binary messages received from the WebSocket connection.
79
+ * @param data - The binary data.
80
+ */
81
+ protected handleBinaryMessage(data: Buffer): void {
82
+ this.emit(AgentEvents.Audio, data);
83
+ }
84
+
85
+ /**
86
+ * Handles text messages received from the WebSocket connection.
87
+ * @param data - The parsed JSON data.
88
+ */
89
+ protected handleTextMessage(data: any): void {
90
+ if (data.type in AgentEvents) {
91
+ this.emit(data.type, data);
92
+ } else {
93
+ this.emit(AgentEvents.Unhandled, data);
94
+ }
95
+ }
96
+
97
+ /**
98
+ * To be called with your model configuration BEFORE sending
99
+ * any audio data.
100
+ * @param options - The SettingsConfiguration object.
101
+ * @param options.audio.input.encoding - The encoding for your inbound (user) audio.
102
+ * @param options.audio.input.sampleRate - The sample rate for your inbound (user) audio.
103
+ * @param options.audio.output.encoding - The encoding for your outbound (agent) audio.
104
+ * @param options.audio.output.sampleRate - The sample rate for your outbound (agent) audio.
105
+ * @param options.audio.output.bitrate - The bitrate for your outbound (agent) audio.
106
+ * @param options.audio.output.container - The container for your outbound (agent) audio.
107
+ * @param options.agent.listen.model - The STT model to use for processing user audio.
108
+ * @param options.agent.speak.model - The TTS model to use for generating agent audio.
109
+ * @param options.agent.think.provider.type - The LLM provider to use.
110
+ * @param options.agent.think.model - The LLM model to use.
111
+ * @param options.agent.think.instructions - The instructions to provide to the LLM.
112
+ * @param options.agent.think.functions - The functions to provide to the LLM.
113
+ * @param options.context.messages - The message history to provide to the LLM (useful if a websocket connection is lost.)
114
+ * @param options.context.replay - Whether to replay the last message if it was an assistant message.
115
+ */
116
+ public configure(options: AgentLiveSchema): void {
117
+ this.send(JSON.stringify({ type: "SettingsConfiguration", options }));
118
+ }
119
+
120
+ /**
121
+ * Provide new instructions to the LLM.
122
+ * @param instructions - The instructions to provide.
123
+ */
124
+ public updateInstructions(instructions: string): void {
125
+ this.send(JSON.stringify({ type: "UpdateInstructions", instructions }));
126
+ }
127
+
128
+ /**
129
+ * Change the speak model.
130
+ * @param model - The new model to use.
131
+ */
132
+ public updateSpeak(model: SpeakModel): void {
133
+ this.send(JSON.stringify({ type: "UpdateSpeak", model }));
134
+ }
135
+
136
+ /**
137
+ * Immediately trigger an agent message. If this message
138
+ * is sent while the user is speaking, or while the server is in the
139
+ * middle of sending audio, then the request will be ignored and an InjectionRefused
140
+ * event will be emitted.
141
+ * @example "Hold on while I look that up for you."
142
+ * @example "Are you still on the line?"
143
+ * @param message - The message to speak.
144
+ */
145
+ public injectAgentMessage(message: string): void {
146
+ this.send(JSON.stringify({ type: "InjectAgentMessage", message }));
147
+ }
148
+
149
+ /**
150
+ * Send a keepalive to avoid closing the websocket while you
151
+ * are not transmitting audio. This should be sent at least
152
+ * every 8 seconds.
153
+ */
154
+ public keepAlive(): void {
155
+ this.send(JSON.stringify({ type: "KeepAlive" }));
156
+ }
157
+ }
@@ -1,6 +1,7 @@
1
1
  export * from "./AbstractClient";
2
2
  export * from "./AbstractLiveClient";
3
3
  export * from "./AbstractRestClient";
4
+ export * from "./AgentLiveClient";
4
5
  export * from "./ListenClient";
5
6
  export * from "./ListenLiveClient";
6
7
  export * from "./ListenRestClient";