@deepgram/sdk 4.6.0 → 4.8.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/README.md +4 -2
- package/dist/main/lib/errors.d.ts +33 -0
- package/dist/main/lib/errors.d.ts.map +1 -1
- package/dist/main/lib/errors.js +35 -1
- package/dist/main/lib/errors.js.map +1 -1
- package/dist/main/lib/helpers.d.ts.map +1 -1
- package/dist/main/lib/helpers.js +7 -10
- package/dist/main/lib/helpers.js.map +1 -1
- package/dist/main/lib/types/AgentLiveSchema.d.ts +1 -1
- package/dist/main/lib/types/AgentLiveSchema.d.ts.map +1 -1
- package/dist/main/lib/types/CreateProjectKeySchema.d.ts +1 -1
- package/dist/main/lib/types/CreateProjectKeySchema.d.ts.map +1 -1
- package/dist/main/lib/types/DeepgramClientOptions.d.ts +6 -6
- package/dist/main/lib/types/DeepgramClientOptions.d.ts.map +1 -1
- package/dist/main/lib/types/DeepgramResponse.d.ts +1 -1
- package/dist/main/lib/types/DeepgramResponse.d.ts.map +1 -1
- package/dist/main/lib/types/DeepgramSource.d.ts +4 -3
- package/dist/main/lib/types/DeepgramSource.d.ts.map +1 -1
- package/dist/main/lib/types/Fetch.d.ts +3 -3
- package/dist/main/lib/types/Fetch.d.ts.map +1 -1
- package/dist/main/lib/types/GetModelsResponse.d.ts +3 -3
- package/dist/main/lib/types/GetModelsResponse.d.ts.map +1 -1
- package/dist/main/lib/types/LiveConfigOptions.d.ts +1 -1
- package/dist/main/lib/types/LiveConfigOptions.d.ts.map +1 -1
- package/dist/main/lib/types/VoidResponse.d.ts +1 -1
- package/dist/main/lib/types/VoidResponse.d.ts.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/main/packages/AbstractLiveClient.d.ts +165 -2
- package/dist/main/packages/AbstractLiveClient.d.ts.map +1 -1
- package/dist/main/packages/AbstractLiveClient.js +162 -0
- package/dist/main/packages/AbstractLiveClient.js.map +1 -1
- package/dist/main/packages/AbstractRestClient.d.ts +1 -0
- package/dist/main/packages/AbstractRestClient.d.ts.map +1 -1
- package/dist/main/packages/AgentLiveClient.d.ts +9 -0
- package/dist/main/packages/AgentLiveClient.d.ts.map +1 -1
- package/dist/main/packages/AgentLiveClient.js +25 -10
- package/dist/main/packages/AgentLiveClient.js.map +1 -1
- package/dist/main/packages/ListenLiveClient.d.ts.map +1 -1
- package/dist/main/packages/ListenLiveClient.js +12 -9
- package/dist/main/packages/ListenLiveClient.js.map +1 -1
- package/dist/main/packages/ManageRestClient.js +1 -1
- package/dist/main/packages/ManageRestClient.js.map +1 -1
- package/dist/main/packages/SpeakLiveClient.d.ts +1 -0
- package/dist/main/packages/SpeakLiveClient.d.ts.map +1 -1
- package/dist/main/packages/SpeakLiveClient.js +15 -9
- package/dist/main/packages/SpeakLiveClient.js.map +1 -1
- package/dist/module/lib/errors.d.ts +33 -0
- package/dist/module/lib/errors.d.ts.map +1 -1
- package/dist/module/lib/errors.js +33 -0
- package/dist/module/lib/errors.js.map +1 -1
- package/dist/module/lib/helpers.d.ts.map +1 -1
- package/dist/module/lib/helpers.js +7 -10
- package/dist/module/lib/helpers.js.map +1 -1
- package/dist/module/lib/types/AgentLiveSchema.d.ts +1 -1
- package/dist/module/lib/types/AgentLiveSchema.d.ts.map +1 -1
- package/dist/module/lib/types/CreateProjectKeySchema.d.ts +1 -1
- package/dist/module/lib/types/CreateProjectKeySchema.d.ts.map +1 -1
- package/dist/module/lib/types/DeepgramClientOptions.d.ts +6 -6
- package/dist/module/lib/types/DeepgramClientOptions.d.ts.map +1 -1
- package/dist/module/lib/types/DeepgramResponse.d.ts +1 -1
- package/dist/module/lib/types/DeepgramResponse.d.ts.map +1 -1
- package/dist/module/lib/types/DeepgramSource.d.ts +4 -3
- package/dist/module/lib/types/DeepgramSource.d.ts.map +1 -1
- package/dist/module/lib/types/Fetch.d.ts +3 -3
- package/dist/module/lib/types/Fetch.d.ts.map +1 -1
- package/dist/module/lib/types/GetModelsResponse.d.ts +3 -3
- package/dist/module/lib/types/GetModelsResponse.d.ts.map +1 -1
- package/dist/module/lib/types/LiveConfigOptions.d.ts +1 -1
- package/dist/module/lib/types/LiveConfigOptions.d.ts.map +1 -1
- package/dist/module/lib/types/VoidResponse.d.ts +1 -1
- package/dist/module/lib/types/VoidResponse.d.ts.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.js +1 -1
- package/dist/module/packages/AbstractLiveClient.d.ts +165 -2
- package/dist/module/packages/AbstractLiveClient.d.ts.map +1 -1
- package/dist/module/packages/AbstractLiveClient.js +162 -0
- package/dist/module/packages/AbstractLiveClient.js.map +1 -1
- package/dist/module/packages/AbstractRestClient.d.ts +1 -0
- package/dist/module/packages/AbstractRestClient.d.ts.map +1 -1
- package/dist/module/packages/AgentLiveClient.d.ts +9 -0
- package/dist/module/packages/AgentLiveClient.d.ts.map +1 -1
- package/dist/module/packages/AgentLiveClient.js +25 -10
- package/dist/module/packages/AgentLiveClient.js.map +1 -1
- package/dist/module/packages/ListenLiveClient.d.ts.map +1 -1
- package/dist/module/packages/ListenLiveClient.js +12 -9
- package/dist/module/packages/ListenLiveClient.js.map +1 -1
- package/dist/module/packages/ManageRestClient.js +1 -1
- package/dist/module/packages/ManageRestClient.js.map +1 -1
- package/dist/module/packages/SpeakLiveClient.d.ts +1 -0
- package/dist/module/packages/SpeakLiveClient.d.ts.map +1 -1
- package/dist/module/packages/SpeakLiveClient.js +15 -9
- package/dist/module/packages/SpeakLiveClient.js.map +1 -1
- package/dist/umd/deepgram.js +2 -1
- package/dist/umd/deepgram.js.LICENSE.txt +10 -0
- package/package.json +21 -15
- package/src/lib/errors.ts +52 -0
- package/src/lib/helpers.ts +6 -10
- package/src/lib/version.ts +1 -1
- package/src/packages/AbstractLiveClient.ts +230 -0
- package/src/packages/AgentLiveClient.ts +27 -13
- package/src/packages/ListenLiveClient.ts +13 -12
- package/src/packages/ManageRestClient.ts +1 -1
- package/src/packages/SpeakLiveClient.ts +16 -12
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* The buffer module from node.js, for the browser.
|
|
3
|
+
*
|
|
4
|
+
* @author Feross Aboukhadijeh <https://feross.org>
|
|
5
|
+
* @license MIT
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
9
|
+
|
|
10
|
+
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepgram/sdk",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.8.0",
|
|
4
4
|
"description": "Isomorphic Javascript client for Deepgram",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"javascript",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"speech",
|
|
11
11
|
"sdk"
|
|
12
12
|
],
|
|
13
|
+
"packageManager": "pnpm@10.12.1",
|
|
13
14
|
"homepage": "https://github.com/deepgram/deepgram-js-sdk",
|
|
14
15
|
"bugs": "https://github.com/deepgram/deepgram-js-sdk/issues",
|
|
15
16
|
"license": "MIT",
|
|
@@ -39,21 +40,26 @@
|
|
|
39
40
|
"repository": "deepgram/deepgram-js-sdk",
|
|
40
41
|
"scripts": {
|
|
41
42
|
"clean": "rimraf dist docs/v2",
|
|
42
|
-
"format": "prettier --write \"
|
|
43
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
43
44
|
"build": "run-s clean format build:*",
|
|
44
45
|
"build:main": "tsc -p tsconfig.json",
|
|
45
46
|
"build:module": "tsc -p tsconfig.module.json",
|
|
46
47
|
"build:umd": "webpack --mode=production",
|
|
47
48
|
"lint": "run-s lint:*",
|
|
48
|
-
"lint:js": "eslint
|
|
49
|
+
"lint:js": "eslint -c .eslintrc-examples.js examples --max-warnings 0",
|
|
49
50
|
"lint:md": "markdownlint **/*.md *.md",
|
|
50
51
|
"lint:yml": "yamllint .github/workflows",
|
|
51
52
|
"lint:ts": "eslint src --max-warnings 0",
|
|
52
53
|
"watch": "nodemon -e ts --watch src --exec \"npm run build\"",
|
|
53
|
-
"test": "mocha -r ts-node/register test/*test.ts test/**/*test.ts --inspect --exit",
|
|
54
|
-
"test:coverage": "nyc --reporter=lcovonly --reporter=text --reporter=text-summary npm run test",
|
|
55
54
|
"docs": "typedoc --entryPoints src/index.ts --out docs/ --includes src/packages/**/*.ts --emit none",
|
|
56
|
-
"docs:json": "typedoc --entryPoints src/index.ts --includes src/packages/**/*.ts --json docs/spec.json --emit none"
|
|
55
|
+
"docs:json": "typedoc --entryPoints src/index.ts --includes src/packages/**/*.ts --json docs/spec.json --emit none",
|
|
56
|
+
"test": "jest",
|
|
57
|
+
"test:unit": "jest --testPathPattern=tests/unit",
|
|
58
|
+
"test:e2e": "jest --testPathPattern=tests/e2e",
|
|
59
|
+
"test:e2e:offline": "node scripts/test-offline.js",
|
|
60
|
+
"test:watch": "jest --watch",
|
|
61
|
+
"test:coverage": "jest --coverage",
|
|
62
|
+
"test:ci": "jest --ci --coverage --watchAll=false"
|
|
57
63
|
},
|
|
58
64
|
"dependencies": {
|
|
59
65
|
"@deepgram/captions": "^1.1.1",
|
|
@@ -66,33 +72,33 @@
|
|
|
66
72
|
"devDependencies": {
|
|
67
73
|
"@commitlint/cli": "^17.6.7",
|
|
68
74
|
"@commitlint/config-conventional": "^17.6.7",
|
|
69
|
-
"@
|
|
70
|
-
"@flydotio/dockerfile": "^0.4.10",
|
|
71
|
-
"@types/chai": "^4.3.5",
|
|
72
|
-
"@types/mocha": "^9.1.1",
|
|
73
|
-
"@types/sinon": "^17.0.3",
|
|
75
|
+
"@types/jest": "^29.5.12",
|
|
74
76
|
"@types/ws": "^8.5.10",
|
|
75
77
|
"@typescript-eslint/eslint-plugin": "^8.7.0",
|
|
76
78
|
"@typescript-eslint/parser": "^8.7.0",
|
|
77
|
-
"
|
|
79
|
+
"buffer": "^6.0.3",
|
|
78
80
|
"cross-env": "^7.0.3",
|
|
81
|
+
"dotenv": "^16.5.0",
|
|
79
82
|
"eslint": "^8.57.1",
|
|
80
83
|
"husky": "^4.3.0",
|
|
84
|
+
"jest": "^29.7.0",
|
|
85
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
81
86
|
"markdownlint": "^0.35.0",
|
|
82
87
|
"markdownlint-cli": "^0.42.0",
|
|
83
|
-
"mocha": "^9.2.2",
|
|
84
88
|
"nodemon": "^3.0.1",
|
|
85
89
|
"npm-run-all": "^4.1.5",
|
|
86
|
-
"nyc": "^15.1.0",
|
|
87
90
|
"prettier": "^2.5.1",
|
|
88
91
|
"pretty-quick": "^3.1.3",
|
|
89
92
|
"rimraf": "^3.0.2",
|
|
90
93
|
"semantic-release-plugin-update-version-in-files": "^1.1.0",
|
|
91
|
-
"
|
|
94
|
+
"stream-browserify": "^3.0.0",
|
|
95
|
+
"ts-jest": "^29.1.2",
|
|
92
96
|
"ts-loader": "^8.0.11",
|
|
93
97
|
"ts-node": "^10.9.1",
|
|
94
98
|
"typedoc": "^0.22.16",
|
|
95
99
|
"typescript": "^4.5.5",
|
|
100
|
+
"url": "^0.11.4",
|
|
101
|
+
"util": "^0.12.5",
|
|
96
102
|
"webpack": "^5.69.1",
|
|
97
103
|
"webpack-cli": "^4.9.2",
|
|
98
104
|
"yaml-lint": "^1.7.0"
|
package/src/lib/errors.ts
CHANGED
|
@@ -48,3 +48,55 @@ export class DeepgramVersionError extends DeepgramError {
|
|
|
48
48
|
this.name = "DeepgramVersionError";
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Enhanced WebSocket error that captures additional debugging information
|
|
54
|
+
* including status codes, request IDs, and response headers when available.
|
|
55
|
+
*/
|
|
56
|
+
export class DeepgramWebSocketError extends DeepgramError {
|
|
57
|
+
originalEvent?: ErrorEvent | Event;
|
|
58
|
+
statusCode?: number;
|
|
59
|
+
requestId?: string;
|
|
60
|
+
responseHeaders?: Record<string, string>;
|
|
61
|
+
url?: string;
|
|
62
|
+
readyState?: number;
|
|
63
|
+
|
|
64
|
+
constructor(
|
|
65
|
+
message: string,
|
|
66
|
+
options: {
|
|
67
|
+
originalEvent?: ErrorEvent | Event;
|
|
68
|
+
statusCode?: number;
|
|
69
|
+
requestId?: string;
|
|
70
|
+
responseHeaders?: Record<string, string>;
|
|
71
|
+
url?: string;
|
|
72
|
+
readyState?: number;
|
|
73
|
+
} = {}
|
|
74
|
+
) {
|
|
75
|
+
super(message);
|
|
76
|
+
this.name = "DeepgramWebSocketError";
|
|
77
|
+
this.originalEvent = options.originalEvent;
|
|
78
|
+
this.statusCode = options.statusCode;
|
|
79
|
+
this.requestId = options.requestId;
|
|
80
|
+
this.responseHeaders = options.responseHeaders;
|
|
81
|
+
this.url = options.url;
|
|
82
|
+
this.readyState = options.readyState;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
toJSON() {
|
|
86
|
+
return {
|
|
87
|
+
name: this.name,
|
|
88
|
+
message: this.message,
|
|
89
|
+
statusCode: this.statusCode,
|
|
90
|
+
requestId: this.requestId,
|
|
91
|
+
responseHeaders: this.responseHeaders,
|
|
92
|
+
url: this.url,
|
|
93
|
+
readyState: this.readyState,
|
|
94
|
+
originalEvent: this.originalEvent
|
|
95
|
+
? {
|
|
96
|
+
type: this.originalEvent.type,
|
|
97
|
+
timeStamp: this.originalEvent.timeStamp,
|
|
98
|
+
}
|
|
99
|
+
: undefined,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
package/src/lib/helpers.ts
CHANGED
|
@@ -47,7 +47,7 @@ export const resolveHeadersConstructor = () => {
|
|
|
47
47
|
export const isUrlSource = (
|
|
48
48
|
providedSource: PrerecordedSource | AnalyzeSource
|
|
49
49
|
): providedSource is UrlSource => {
|
|
50
|
-
if ((providedSource as UrlSource).url) return true;
|
|
50
|
+
if (providedSource && (providedSource as UrlSource).url) return true;
|
|
51
51
|
|
|
52
52
|
return false;
|
|
53
53
|
};
|
|
@@ -55,7 +55,7 @@ export const isUrlSource = (
|
|
|
55
55
|
export const isTextSource = (
|
|
56
56
|
providedSource: PrerecordedSource | AnalyzeSource
|
|
57
57
|
): providedSource is TextSource => {
|
|
58
|
-
if ((providedSource as TextSource).text) return true;
|
|
58
|
+
if (providedSource && (providedSource as TextSource).text) return true;
|
|
59
59
|
|
|
60
60
|
return false;
|
|
61
61
|
};
|
|
@@ -67,15 +67,11 @@ export const isFileSource = (providedSource: PrerecordedSource): providedSource
|
|
|
67
67
|
};
|
|
68
68
|
|
|
69
69
|
const isBufferSource = (providedSource: PrerecordedSource): providedSource is Buffer => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return false;
|
|
70
|
+
return providedSource != null && Buffer.isBuffer(providedSource);
|
|
73
71
|
};
|
|
74
72
|
|
|
75
73
|
const isReadStreamSource = (providedSource: PrerecordedSource): providedSource is Readable => {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return false;
|
|
74
|
+
return providedSource != null && providedSource instanceof Readable;
|
|
79
75
|
};
|
|
80
76
|
|
|
81
77
|
export class CallbackUrl extends URL {
|
|
@@ -100,11 +96,11 @@ export const buildRequestUrl = (
|
|
|
100
96
|
};
|
|
101
97
|
|
|
102
98
|
export function isLiveSchema(arg: any): arg is LiveSchema {
|
|
103
|
-
return arg && typeof arg.interim_results !== "undefined";
|
|
99
|
+
return arg != null && typeof arg.interim_results !== "undefined";
|
|
104
100
|
}
|
|
105
101
|
|
|
106
102
|
export function isDeepgramClientOptions(arg: any): arg is DeepgramClientOptions {
|
|
107
|
-
return arg && typeof arg.global !== "undefined";
|
|
103
|
+
return arg != null && typeof arg.global !== "undefined";
|
|
108
104
|
}
|
|
109
105
|
|
|
110
106
|
export const convertLegacyOptions = (optionsArg: DeepgramClientOptions): DeepgramClientOptions => {
|
package/src/lib/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "4.
|
|
1
|
+
export const version = "4.8.0";
|
|
@@ -3,6 +3,7 @@ import { CONNECTION_STATE, SOCKET_STATES } from "../lib/constants";
|
|
|
3
3
|
import type { DeepgramClientOptions, LiveSchema } from "../lib/types";
|
|
4
4
|
import type { WebSocket as WSWebSocket } from "ws";
|
|
5
5
|
import { isBun } from "../lib/runtime";
|
|
6
|
+
import { DeepgramWebSocketError } from "../lib/errors";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Represents a constructor for a WebSocket-like object that can be used in the application.
|
|
@@ -285,6 +286,235 @@ export abstract class AbstractLiveClient extends AbstractClient {
|
|
|
285
286
|
return this.key === "proxy" && !!this.namespaceOptions.websocket.options.proxy?.url;
|
|
286
287
|
}
|
|
287
288
|
|
|
289
|
+
/**
|
|
290
|
+
* Extracts enhanced error information from a WebSocket error event.
|
|
291
|
+
* This method attempts to capture additional debugging information such as
|
|
292
|
+
* status codes, request IDs, and response headers when available.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```typescript
|
|
296
|
+
* // Enhanced error information is now available in error events:
|
|
297
|
+
* connection.on(LiveTranscriptionEvents.Error, (err) => {
|
|
298
|
+
* console.error("WebSocket Error:", err.message);
|
|
299
|
+
*
|
|
300
|
+
* // Access HTTP status code (e.g., 502, 403, etc.)
|
|
301
|
+
* if (err.statusCode) {
|
|
302
|
+
* console.error(`HTTP Status Code: ${err.statusCode}`);
|
|
303
|
+
* }
|
|
304
|
+
*
|
|
305
|
+
* // Access Deepgram request ID for support tickets
|
|
306
|
+
* if (err.requestId) {
|
|
307
|
+
* console.error(`Deepgram Request ID: ${err.requestId}`);
|
|
308
|
+
* }
|
|
309
|
+
*
|
|
310
|
+
* // Access WebSocket URL and connection state
|
|
311
|
+
* if (err.url) {
|
|
312
|
+
* console.error(`WebSocket URL: ${err.url}`);
|
|
313
|
+
* }
|
|
314
|
+
*
|
|
315
|
+
* if (err.readyState !== undefined) {
|
|
316
|
+
* const stateNames = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
|
|
317
|
+
* console.error(`Connection State: ${stateNames[err.readyState]}`);
|
|
318
|
+
* }
|
|
319
|
+
*
|
|
320
|
+
* // Access response headers for additional debugging
|
|
321
|
+
* if (err.responseHeaders) {
|
|
322
|
+
* console.error("Response Headers:", err.responseHeaders);
|
|
323
|
+
* }
|
|
324
|
+
*
|
|
325
|
+
* // Access the enhanced error object for detailed debugging
|
|
326
|
+
* if (err.error?.name === 'DeepgramWebSocketError') {
|
|
327
|
+
* console.error("Enhanced Error Details:", err.error.toJSON());
|
|
328
|
+
* }
|
|
329
|
+
* });
|
|
330
|
+
* ```
|
|
331
|
+
*
|
|
332
|
+
* @param event - The error event from the WebSocket
|
|
333
|
+
* @param conn - The WebSocket connection object
|
|
334
|
+
* @returns Enhanced error information object
|
|
335
|
+
*/
|
|
336
|
+
protected extractErrorInformation(
|
|
337
|
+
event: ErrorEvent | Event,
|
|
338
|
+
conn?: WebSocketLike
|
|
339
|
+
): {
|
|
340
|
+
statusCode?: number;
|
|
341
|
+
requestId?: string;
|
|
342
|
+
responseHeaders?: Record<string, string>;
|
|
343
|
+
url?: string;
|
|
344
|
+
readyState?: number;
|
|
345
|
+
} {
|
|
346
|
+
const errorInfo: {
|
|
347
|
+
statusCode?: number;
|
|
348
|
+
requestId?: string;
|
|
349
|
+
responseHeaders?: Record<string, string>;
|
|
350
|
+
url?: string;
|
|
351
|
+
readyState?: number;
|
|
352
|
+
} = {};
|
|
353
|
+
|
|
354
|
+
// Extract basic connection information
|
|
355
|
+
if (conn) {
|
|
356
|
+
errorInfo.readyState = conn.readyState;
|
|
357
|
+
errorInfo.url = typeof conn.url === "string" ? conn.url : conn.url?.toString();
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Try to extract additional information from the WebSocket connection
|
|
361
|
+
// This works with the 'ws' package which exposes more detailed error information
|
|
362
|
+
if (conn && typeof conn === "object") {
|
|
363
|
+
const wsConn = conn as any;
|
|
364
|
+
|
|
365
|
+
// Extract status code if available (from 'ws' package)
|
|
366
|
+
if (wsConn._req && wsConn._req.res) {
|
|
367
|
+
errorInfo.statusCode = wsConn._req.res.statusCode;
|
|
368
|
+
|
|
369
|
+
// Extract response headers if available
|
|
370
|
+
if (wsConn._req.res.headers) {
|
|
371
|
+
errorInfo.responseHeaders = { ...wsConn._req.res.headers };
|
|
372
|
+
|
|
373
|
+
// Extract request ID from Deepgram response headers
|
|
374
|
+
const requestId =
|
|
375
|
+
wsConn._req.res.headers["dg-request-id"] || wsConn._req.res.headers["x-dg-request-id"];
|
|
376
|
+
if (requestId) {
|
|
377
|
+
errorInfo.requestId = requestId;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// For native WebSocket, try to extract information from the event
|
|
383
|
+
if (event && "target" in event && event.target) {
|
|
384
|
+
const target = event.target as any;
|
|
385
|
+
if (target.url) {
|
|
386
|
+
errorInfo.url = target.url;
|
|
387
|
+
}
|
|
388
|
+
if (target.readyState !== undefined) {
|
|
389
|
+
errorInfo.readyState = target.readyState;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return errorInfo;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Creates an enhanced error object with additional debugging information.
|
|
399
|
+
* This method provides backward compatibility by including both the original
|
|
400
|
+
* error event and enhanced error information.
|
|
401
|
+
*
|
|
402
|
+
* @param event - The original error event
|
|
403
|
+
* @param enhancedInfo - Additional error information extracted from the connection
|
|
404
|
+
* @returns An object containing both original and enhanced error information
|
|
405
|
+
*/
|
|
406
|
+
protected createEnhancedError(
|
|
407
|
+
event: ErrorEvent | Event,
|
|
408
|
+
enhancedInfo: {
|
|
409
|
+
statusCode?: number;
|
|
410
|
+
requestId?: string;
|
|
411
|
+
responseHeaders?: Record<string, string>;
|
|
412
|
+
url?: string;
|
|
413
|
+
readyState?: number;
|
|
414
|
+
}
|
|
415
|
+
) {
|
|
416
|
+
// Create the enhanced error for detailed debugging
|
|
417
|
+
const enhancedError = new DeepgramWebSocketError(
|
|
418
|
+
(event as ErrorEvent).message || "WebSocket connection error",
|
|
419
|
+
{
|
|
420
|
+
originalEvent: event,
|
|
421
|
+
...enhancedInfo,
|
|
422
|
+
}
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
// Return an object that maintains backward compatibility
|
|
426
|
+
// while providing enhanced information
|
|
427
|
+
return {
|
|
428
|
+
// Original event for backward compatibility
|
|
429
|
+
...event,
|
|
430
|
+
// Enhanced error information
|
|
431
|
+
error: enhancedError,
|
|
432
|
+
// Additional fields for easier access
|
|
433
|
+
statusCode: enhancedInfo.statusCode,
|
|
434
|
+
requestId: enhancedInfo.requestId,
|
|
435
|
+
responseHeaders: enhancedInfo.responseHeaders,
|
|
436
|
+
url: enhancedInfo.url,
|
|
437
|
+
readyState: enhancedInfo.readyState,
|
|
438
|
+
// Enhanced message with more context
|
|
439
|
+
message: this.buildEnhancedErrorMessage(event, enhancedInfo),
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Builds an enhanced error message with additional context information.
|
|
445
|
+
*
|
|
446
|
+
* @param event - The original error event
|
|
447
|
+
* @param enhancedInfo - Additional error information
|
|
448
|
+
* @returns A more descriptive error message
|
|
449
|
+
*/
|
|
450
|
+
protected buildEnhancedErrorMessage(
|
|
451
|
+
event: ErrorEvent | Event,
|
|
452
|
+
enhancedInfo: {
|
|
453
|
+
statusCode?: number;
|
|
454
|
+
requestId?: string;
|
|
455
|
+
responseHeaders?: Record<string, string>;
|
|
456
|
+
url?: string;
|
|
457
|
+
readyState?: number;
|
|
458
|
+
}
|
|
459
|
+
): string {
|
|
460
|
+
let message = (event as ErrorEvent).message || "WebSocket connection error";
|
|
461
|
+
|
|
462
|
+
const details: string[] = [];
|
|
463
|
+
|
|
464
|
+
if (enhancedInfo.statusCode) {
|
|
465
|
+
details.push(`Status: ${enhancedInfo.statusCode}`);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (enhancedInfo.requestId) {
|
|
469
|
+
details.push(`Request ID: ${enhancedInfo.requestId}`);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (enhancedInfo.readyState !== undefined) {
|
|
473
|
+
const stateNames = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
|
|
474
|
+
const stateName =
|
|
475
|
+
stateNames[enhancedInfo.readyState] || `Unknown(${enhancedInfo.readyState})`;
|
|
476
|
+
details.push(`Ready State: ${stateName}`);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (enhancedInfo.url) {
|
|
480
|
+
details.push(`URL: ${enhancedInfo.url}`);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (details.length > 0) {
|
|
484
|
+
message += ` (${details.join(", ")})`;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
return message;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Sets up the standard connection event handlers (open, close, error) for WebSocket connections.
|
|
492
|
+
* This method abstracts the common connection event registration pattern used across all live clients.
|
|
493
|
+
*
|
|
494
|
+
* @param events - Object containing the event constants for the specific client type
|
|
495
|
+
* @param events.Open - Event constant for connection open
|
|
496
|
+
* @param events.Close - Event constant for connection close
|
|
497
|
+
* @param events.Error - Event constant for connection error
|
|
498
|
+
* @protected
|
|
499
|
+
*/
|
|
500
|
+
protected setupConnectionEvents(events: { Open: string; Close: string; Error: string }): void {
|
|
501
|
+
if (this.conn) {
|
|
502
|
+
this.conn.onopen = () => {
|
|
503
|
+
this.emit(events.Open, this);
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
this.conn.onclose = (event: any) => {
|
|
507
|
+
this.emit(events.Close, event);
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
this.conn.onerror = (event: ErrorEvent) => {
|
|
511
|
+
const enhancedInfo = this.extractErrorInformation(event, this.conn || undefined);
|
|
512
|
+
const enhancedError = this.createEnhancedError(event, enhancedInfo);
|
|
513
|
+
this.emit(events.Error, enhancedError);
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
288
518
|
/**
|
|
289
519
|
* Sets up the connection event handlers.
|
|
290
520
|
*
|
|
@@ -22,19 +22,15 @@ export class AgentLiveClient extends AbstractLiveClient {
|
|
|
22
22
|
* - When a message is received, it parses the message and emits the appropriate event based on the message type.
|
|
23
23
|
*/
|
|
24
24
|
public setupConnection(): void {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
this.emit(AgentEvents.Close, event);
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
this.conn.onerror = (event: ErrorEvent) => {
|
|
35
|
-
this.emit(AgentEvents.Error, event);
|
|
36
|
-
};
|
|
25
|
+
// Set up standard connection events (open, close, error) using abstracted method
|
|
26
|
+
this.setupConnectionEvents({
|
|
27
|
+
Open: AgentEvents.Open,
|
|
28
|
+
Close: AgentEvents.Close,
|
|
29
|
+
Error: AgentEvents.Error,
|
|
30
|
+
});
|
|
37
31
|
|
|
32
|
+
// Set up message handling specific to agent conversations
|
|
33
|
+
if (this.conn) {
|
|
38
34
|
this.conn.onmessage = (event: MessageEvent) => {
|
|
39
35
|
this.handleMessage(event);
|
|
40
36
|
};
|
|
@@ -53,9 +49,13 @@ export class AgentLiveClient extends AbstractLiveClient {
|
|
|
53
49
|
} catch (error) {
|
|
54
50
|
this.emit(AgentEvents.Error, {
|
|
55
51
|
event,
|
|
56
|
-
data:
|
|
52
|
+
data:
|
|
53
|
+
event.data?.toString().substring(0, 200) +
|
|
54
|
+
(event.data?.toString().length > 200 ? "..." : ""),
|
|
57
55
|
message: "Unable to parse `data` as JSON.",
|
|
58
56
|
error,
|
|
57
|
+
url: this.conn?.url,
|
|
58
|
+
readyState: this.conn?.readyState,
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
} else if (event.data instanceof Blob) {
|
|
@@ -71,6 +71,9 @@ export class AgentLiveClient extends AbstractLiveClient {
|
|
|
71
71
|
this.emit(AgentEvents.Error, {
|
|
72
72
|
event,
|
|
73
73
|
message: "Received unknown data type.",
|
|
74
|
+
url: this.conn?.url,
|
|
75
|
+
readyState: this.conn?.readyState,
|
|
76
|
+
dataType: typeof event.data,
|
|
74
77
|
});
|
|
75
78
|
}
|
|
76
79
|
}
|
|
@@ -137,6 +140,17 @@ export class AgentLiveClient extends AbstractLiveClient {
|
|
|
137
140
|
this.send(JSON.stringify({ type: "InjectAgentMessage", content }));
|
|
138
141
|
}
|
|
139
142
|
|
|
143
|
+
/**
|
|
144
|
+
* Send a text-based message to the agent as if it came from the user.
|
|
145
|
+
* This allows you to inject user messages into the conversation for the agent to respond to.
|
|
146
|
+
* @example "Hello! Can you hear me?"
|
|
147
|
+
* @example "What's the weather like today?"
|
|
148
|
+
* @param content - The specific phrase or statement the agent should respond to.
|
|
149
|
+
*/
|
|
150
|
+
public injectUserMessage(content: string): void {
|
|
151
|
+
this.send(JSON.stringify({ type: "InjectUserMessage", content }));
|
|
152
|
+
}
|
|
153
|
+
|
|
140
154
|
/**
|
|
141
155
|
* Respond to a function call request.
|
|
142
156
|
* @param response - The response to the function call request.
|
|
@@ -51,19 +51,15 @@ export class ListenLiveClient extends AbstractLiveClient {
|
|
|
51
51
|
* - When a message is received, it parses the message and emits the appropriate event based on the message type, such as `LiveTranscriptionEvents.Metadata`, `LiveTranscriptionEvents.Transcript`, `LiveTranscriptionEvents.UtteranceEnd`, and `LiveTranscriptionEvents.SpeechStarted`.
|
|
52
52
|
*/
|
|
53
53
|
public setupConnection(): void {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.emit(LiveTranscriptionEvents.Close, event);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
this.conn.onerror = (event: ErrorEvent) => {
|
|
64
|
-
this.emit(LiveTranscriptionEvents.Error, event);
|
|
65
|
-
};
|
|
54
|
+
// Set up standard connection events (open, close, error) using abstracted method
|
|
55
|
+
this.setupConnectionEvents({
|
|
56
|
+
Open: LiveTranscriptionEvents.Open,
|
|
57
|
+
Close: LiveTranscriptionEvents.Close,
|
|
58
|
+
Error: LiveTranscriptionEvents.Error,
|
|
59
|
+
});
|
|
66
60
|
|
|
61
|
+
// Set up message handling specific to transcription
|
|
62
|
+
if (this.conn) {
|
|
67
63
|
this.conn.onmessage = (event: MessageEvent) => {
|
|
68
64
|
try {
|
|
69
65
|
const data: any = JSON.parse(event.data.toString());
|
|
@@ -84,6 +80,11 @@ export class ListenLiveClient extends AbstractLiveClient {
|
|
|
84
80
|
event,
|
|
85
81
|
message: "Unable to parse `data` as JSON.",
|
|
86
82
|
error,
|
|
83
|
+
url: this.conn?.url,
|
|
84
|
+
readyState: this.conn?.readyState,
|
|
85
|
+
data:
|
|
86
|
+
event.data?.toString().substring(0, 200) +
|
|
87
|
+
(event.data?.toString().length > 200 ? "..." : ""),
|
|
87
88
|
});
|
|
88
89
|
}
|
|
89
90
|
};
|
|
@@ -515,7 +515,7 @@ export class ManageRestClient extends AbstractRestClient {
|
|
|
515
515
|
): Promise<VoidResponse> {
|
|
516
516
|
try {
|
|
517
517
|
const requestUrl = this.getRequestUrl(endpoint, { projectId, email });
|
|
518
|
-
await this.delete(requestUrl)
|
|
518
|
+
await this.delete(requestUrl);
|
|
519
519
|
|
|
520
520
|
return { error: null };
|
|
521
521
|
} catch (error) {
|
|
@@ -42,19 +42,15 @@ export class SpeakLiveClient extends AbstractLiveClient {
|
|
|
42
42
|
* - When a message is received, it parses the message and emits the appropriate event based on the message type, such as `LiveTTSEvents.Metadata`, `LiveTTSEvents.Flushed`, and `LiveTTSEvents.Warning`.
|
|
43
43
|
*/
|
|
44
44
|
public setupConnection(): void {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
this.emit(LiveTTSEvents.Close, event);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
this.conn.onerror = (event: ErrorEvent) => {
|
|
55
|
-
this.emit(LiveTTSEvents.Error, event);
|
|
56
|
-
};
|
|
45
|
+
// Set up standard connection events (open, close, error) using abstracted method
|
|
46
|
+
this.setupConnectionEvents({
|
|
47
|
+
Open: LiveTTSEvents.Open,
|
|
48
|
+
Close: LiveTTSEvents.Close,
|
|
49
|
+
Error: LiveTTSEvents.Error,
|
|
50
|
+
});
|
|
57
51
|
|
|
52
|
+
// Set up message handling specific to text-to-speech
|
|
53
|
+
if (this.conn) {
|
|
58
54
|
this.conn.onmessage = (event: MessageEvent) => {
|
|
59
55
|
this.handleMessage(event);
|
|
60
56
|
};
|
|
@@ -146,6 +142,11 @@ export class SpeakLiveClient extends AbstractLiveClient {
|
|
|
146
142
|
event,
|
|
147
143
|
message: "Unable to parse `data` as JSON.",
|
|
148
144
|
error,
|
|
145
|
+
url: this.conn?.url,
|
|
146
|
+
readyState: this.conn?.readyState,
|
|
147
|
+
data:
|
|
148
|
+
event.data?.toString().substring(0, 200) +
|
|
149
|
+
(event.data?.toString().length > 200 ? "..." : ""),
|
|
149
150
|
});
|
|
150
151
|
}
|
|
151
152
|
} else if (event.data instanceof Blob) {
|
|
@@ -161,6 +162,9 @@ export class SpeakLiveClient extends AbstractLiveClient {
|
|
|
161
162
|
this.emit(LiveTTSEvents.Error, {
|
|
162
163
|
event,
|
|
163
164
|
message: "Received unknown data type.",
|
|
165
|
+
url: this.conn?.url,
|
|
166
|
+
readyState: this.conn?.readyState,
|
|
167
|
+
dataType: typeof event.data,
|
|
164
168
|
});
|
|
165
169
|
}
|
|
166
170
|
}
|