@deepgram/sdk 5.0.0-alpha.3 → 5.0.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 (157) hide show
  1. package/LICENSE +1 -1
  2. package/dist/browser/index.global.js +496 -1104
  3. package/dist/cjs/BaseClient.d.ts +7 -7
  4. package/dist/cjs/BaseClient.js +2 -2
  5. package/dist/cjs/Client.d.ts +1 -2
  6. package/dist/cjs/CustomClient.js +253 -249
  7. package/dist/cjs/api/resources/agent/client/Client.d.ts +1 -2
  8. package/dist/cjs/api/resources/agent/resources/v1/client/Client.d.ts +1 -2
  9. package/dist/cjs/api/resources/agent/resources/v1/client/Socket.d.ts +8 -8
  10. package/dist/cjs/api/resources/agent/resources/v1/client/Socket.js +9 -9
  11. package/dist/cjs/api/resources/agent/resources/v1/resources/settings/client/Client.d.ts +1 -2
  12. package/dist/cjs/api/resources/agent/resources/v1/resources/settings/resources/think/client/Client.d.ts +1 -2
  13. package/dist/cjs/api/resources/agent/resources/v1/resources/settings/resources/think/resources/models/client/Client.d.ts +1 -2
  14. package/dist/cjs/api/resources/auth/client/Client.d.ts +1 -2
  15. package/dist/cjs/api/resources/auth/resources/v1/client/Client.d.ts +1 -2
  16. package/dist/cjs/api/resources/auth/resources/v1/resources/tokens/client/Client.d.ts +1 -2
  17. package/dist/cjs/api/resources/listen/client/Client.d.ts +1 -2
  18. package/dist/cjs/api/resources/listen/resources/v1/client/Client.d.ts +1 -2
  19. package/dist/cjs/api/resources/listen/resources/v1/client/Client.js +29 -80
  20. package/dist/cjs/api/resources/listen/resources/v1/client/Socket.d.ts +4 -4
  21. package/dist/cjs/api/resources/listen/resources/v1/client/Socket.js +5 -5
  22. package/dist/cjs/api/resources/listen/resources/v1/resources/media/client/Client.d.ts +1 -2
  23. package/dist/cjs/api/resources/listen/resources/v1/resources/media/client/Client.js +76 -298
  24. package/dist/cjs/api/resources/listen/resources/v2/client/Client.d.ts +1 -2
  25. package/dist/cjs/api/resources/listen/resources/v2/client/Client.js +11 -26
  26. package/dist/cjs/api/resources/listen/resources/v2/client/Socket.d.ts +2 -2
  27. package/dist/cjs/api/resources/listen/resources/v2/client/Socket.js +3 -3
  28. package/dist/cjs/api/resources/manage/client/Client.d.ts +1 -2
  29. package/dist/cjs/api/resources/manage/resources/v1/client/Client.d.ts +1 -2
  30. package/dist/cjs/api/resources/manage/resources/v1/resources/models/client/Client.d.ts +1 -2
  31. package/dist/cjs/api/resources/manage/resources/v1/resources/models/client/Client.js +3 -4
  32. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/client/Client.d.ts +1 -2
  33. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/client/Client.js +4 -7
  34. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/billing/client/Client.d.ts +1 -2
  35. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/balances/client/Client.d.ts +1 -2
  36. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/breakdown/client/Client.d.ts +1 -2
  37. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/breakdown/client/Client.js +9 -27
  38. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/fields/client/Client.d.ts +1 -2
  39. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/fields/client/Client.js +4 -7
  40. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/purchases/client/Client.d.ts +1 -2
  41. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/purchases/client/Client.js +3 -4
  42. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/keys/client/Client.d.ts +1 -2
  43. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/keys/client/Client.js +3 -4
  44. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/members/client/Client.d.ts +1 -2
  45. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/members/resources/invites/client/Client.d.ts +1 -2
  46. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/members/resources/scopes/client/Client.d.ts +1 -2
  47. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/models/client/Client.d.ts +1 -2
  48. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/models/client/Client.js +3 -4
  49. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/requests/client/Client.d.ts +1 -2
  50. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/requests/client/Client.js +12 -31
  51. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/usage/client/Client.d.ts +1 -2
  52. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/usage/client/Client.js +46 -133
  53. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/usage/resources/breakdown/client/Client.d.ts +1 -2
  54. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/usage/resources/breakdown/client/Client.js +47 -136
  55. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/usage/resources/fields/client/Client.d.ts +1 -2
  56. package/dist/cjs/api/resources/manage/resources/v1/resources/projects/resources/usage/resources/fields/client/Client.js +4 -7
  57. package/dist/cjs/api/resources/read/client/Client.d.ts +1 -2
  58. package/dist/cjs/api/resources/read/resources/v1/client/Client.d.ts +1 -2
  59. package/dist/cjs/api/resources/read/resources/v1/resources/text/client/Client.d.ts +1 -2
  60. package/dist/cjs/api/resources/read/resources/v1/resources/text/client/Client.js +14 -52
  61. package/dist/cjs/api/resources/selfHosted/client/Client.d.ts +1 -2
  62. package/dist/cjs/api/resources/selfHosted/resources/v1/client/Client.d.ts +1 -2
  63. package/dist/cjs/api/resources/selfHosted/resources/v1/resources/distributionCredentials/client/Client.d.ts +1 -2
  64. package/dist/cjs/api/resources/selfHosted/resources/v1/resources/distributionCredentials/client/Client.js +4 -12
  65. package/dist/cjs/api/resources/speak/client/Client.d.ts +1 -2
  66. package/dist/cjs/api/resources/speak/resources/v1/client/Client.d.ts +1 -2
  67. package/dist/cjs/api/resources/speak/resources/v1/client/Client.js +6 -13
  68. package/dist/cjs/api/resources/speak/resources/v1/client/Socket.d.ts +4 -4
  69. package/dist/cjs/api/resources/speak/resources/v1/client/Socket.js +4 -4
  70. package/dist/cjs/api/resources/speak/resources/v1/resources/audio/client/Client.d.ts +1 -2
  71. package/dist/cjs/api/resources/speak/resources/v1/resources/audio/client/Client.js +11 -33
  72. package/dist/cjs/auth/HeaderAuthProvider.d.ts +14 -8
  73. package/dist/cjs/auth/HeaderAuthProvider.js +19 -9
  74. package/dist/cjs/core/fetcher/EndpointSupplier.d.ts +2 -2
  75. package/dist/cjs/core/fetcher/Fetcher.d.ts +1 -2
  76. package/dist/cjs/core/headers.d.ts +2 -2
  77. package/dist/cjs/core/websocket/ws.d.ts +1 -1
  78. package/dist/cjs/version.d.ts +1 -1
  79. package/dist/cjs/version.js +1 -1
  80. package/dist/esm/BaseClient.d.mts +7 -7
  81. package/dist/esm/BaseClient.mjs +2 -2
  82. package/dist/esm/Client.d.mts +1 -2
  83. package/dist/esm/CustomClient.mjs +254 -250
  84. package/dist/esm/api/resources/agent/client/Client.d.mts +1 -2
  85. package/dist/esm/api/resources/agent/resources/v1/client/Client.d.mts +1 -2
  86. package/dist/esm/api/resources/agent/resources/v1/client/Socket.d.mts +8 -8
  87. package/dist/esm/api/resources/agent/resources/v1/client/Socket.mjs +9 -9
  88. package/dist/esm/api/resources/agent/resources/v1/resources/settings/client/Client.d.mts +1 -2
  89. package/dist/esm/api/resources/agent/resources/v1/resources/settings/resources/think/client/Client.d.mts +1 -2
  90. package/dist/esm/api/resources/agent/resources/v1/resources/settings/resources/think/resources/models/client/Client.d.mts +1 -2
  91. package/dist/esm/api/resources/auth/client/Client.d.mts +1 -2
  92. package/dist/esm/api/resources/auth/resources/v1/client/Client.d.mts +1 -2
  93. package/dist/esm/api/resources/auth/resources/v1/resources/tokens/client/Client.d.mts +1 -2
  94. package/dist/esm/api/resources/listen/client/Client.d.mts +1 -2
  95. package/dist/esm/api/resources/listen/resources/v1/client/Client.d.mts +1 -2
  96. package/dist/esm/api/resources/listen/resources/v1/client/Client.mjs +29 -80
  97. package/dist/esm/api/resources/listen/resources/v1/client/Socket.d.mts +4 -4
  98. package/dist/esm/api/resources/listen/resources/v1/client/Socket.mjs +5 -5
  99. package/dist/esm/api/resources/listen/resources/v1/resources/media/client/Client.d.mts +1 -2
  100. package/dist/esm/api/resources/listen/resources/v1/resources/media/client/Client.mjs +76 -298
  101. package/dist/esm/api/resources/listen/resources/v2/client/Client.d.mts +1 -2
  102. package/dist/esm/api/resources/listen/resources/v2/client/Client.mjs +11 -26
  103. package/dist/esm/api/resources/listen/resources/v2/client/Socket.d.mts +2 -2
  104. package/dist/esm/api/resources/listen/resources/v2/client/Socket.mjs +3 -3
  105. package/dist/esm/api/resources/manage/client/Client.d.mts +1 -2
  106. package/dist/esm/api/resources/manage/resources/v1/client/Client.d.mts +1 -2
  107. package/dist/esm/api/resources/manage/resources/v1/resources/models/client/Client.d.mts +1 -2
  108. package/dist/esm/api/resources/manage/resources/v1/resources/models/client/Client.mjs +3 -4
  109. package/dist/esm/api/resources/manage/resources/v1/resources/projects/client/Client.d.mts +1 -2
  110. package/dist/esm/api/resources/manage/resources/v1/resources/projects/client/Client.mjs +4 -7
  111. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/billing/client/Client.d.mts +1 -2
  112. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/balances/client/Client.d.mts +1 -2
  113. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/breakdown/client/Client.d.mts +1 -2
  114. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/breakdown/client/Client.mjs +9 -27
  115. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/fields/client/Client.d.mts +1 -2
  116. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/fields/client/Client.mjs +4 -7
  117. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/purchases/client/Client.d.mts +1 -2
  118. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/billing/resources/purchases/client/Client.mjs +3 -4
  119. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/keys/client/Client.d.mts +1 -2
  120. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/keys/client/Client.mjs +3 -4
  121. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/members/client/Client.d.mts +1 -2
  122. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/members/resources/invites/client/Client.d.mts +1 -2
  123. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/members/resources/scopes/client/Client.d.mts +1 -2
  124. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/models/client/Client.d.mts +1 -2
  125. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/models/client/Client.mjs +3 -4
  126. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/requests/client/Client.d.mts +1 -2
  127. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/requests/client/Client.mjs +12 -31
  128. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/usage/client/Client.d.mts +1 -2
  129. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/usage/client/Client.mjs +46 -133
  130. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/usage/resources/breakdown/client/Client.d.mts +1 -2
  131. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/usage/resources/breakdown/client/Client.mjs +47 -136
  132. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/usage/resources/fields/client/Client.d.mts +1 -2
  133. package/dist/esm/api/resources/manage/resources/v1/resources/projects/resources/usage/resources/fields/client/Client.mjs +4 -7
  134. package/dist/esm/api/resources/read/client/Client.d.mts +1 -2
  135. package/dist/esm/api/resources/read/resources/v1/client/Client.d.mts +1 -2
  136. package/dist/esm/api/resources/read/resources/v1/resources/text/client/Client.d.mts +1 -2
  137. package/dist/esm/api/resources/read/resources/v1/resources/text/client/Client.mjs +14 -52
  138. package/dist/esm/api/resources/selfHosted/client/Client.d.mts +1 -2
  139. package/dist/esm/api/resources/selfHosted/resources/v1/client/Client.d.mts +1 -2
  140. package/dist/esm/api/resources/selfHosted/resources/v1/resources/distributionCredentials/client/Client.d.mts +1 -2
  141. package/dist/esm/api/resources/selfHosted/resources/v1/resources/distributionCredentials/client/Client.mjs +4 -12
  142. package/dist/esm/api/resources/speak/client/Client.d.mts +1 -2
  143. package/dist/esm/api/resources/speak/resources/v1/client/Client.d.mts +1 -2
  144. package/dist/esm/api/resources/speak/resources/v1/client/Client.mjs +6 -13
  145. package/dist/esm/api/resources/speak/resources/v1/client/Socket.d.mts +4 -4
  146. package/dist/esm/api/resources/speak/resources/v1/client/Socket.mjs +4 -4
  147. package/dist/esm/api/resources/speak/resources/v1/resources/audio/client/Client.d.mts +1 -2
  148. package/dist/esm/api/resources/speak/resources/v1/resources/audio/client/Client.mjs +11 -33
  149. package/dist/esm/auth/HeaderAuthProvider.d.mts +14 -8
  150. package/dist/esm/auth/HeaderAuthProvider.mjs +19 -9
  151. package/dist/esm/core/fetcher/EndpointSupplier.d.mts +2 -2
  152. package/dist/esm/core/fetcher/Fetcher.d.mts +1 -2
  153. package/dist/esm/core/headers.d.mts +2 -2
  154. package/dist/esm/core/websocket/ws.d.mts +1 -1
  155. package/dist/esm/version.d.mts +1 -1
  156. package/dist/esm/version.mjs +1 -1
  157. package/package.json +10 -7
@@ -20,11 +20,13 @@ import { V1Socket as AgentV1Socket } from "./api/resources/agent/resources/v1/cl
20
20
  import { V1Socket as ListenV1Socket } from "./api/resources/listen/resources/v1/client/Socket.mjs";
21
21
  import { V2Socket as ListenV2Socket } from "./api/resources/listen/resources/v2/client/Socket.mjs";
22
22
  import { V1Socket as SpeakV1Socket } from "./api/resources/speak/resources/v1/client/Socket.mjs";
23
- import { mergeHeaders, mergeOnlyDefinedHeaders } from "./core/headers.mjs";
23
+ import { mergeHeaders } from "./core/headers.mjs";
24
24
  import { fromJson } from "./core/json.mjs";
25
25
  import * as core from "./core/index.mjs";
26
26
  import * as environments from "./environments.mjs";
27
27
  import { RUNTIME } from "./core/runtime/index.mjs";
28
+ // Default WebSocket connection timeout in milliseconds
29
+ const DEFAULT_CONNECTION_TIMEOUT_MS = 10000;
28
30
  // Import ws library for Node.js (will be undefined in browser)
29
31
  let NodeWebSocket;
30
32
  try {
@@ -39,29 +41,55 @@ catch (_a) {
39
41
  }
40
42
  // Helper function to generate UUID that works in both Node.js and browser
41
43
  function generateUUID() {
42
- // In Node.js, use the crypto module
44
+ // Try global crypto.randomUUID first (works in both Node.js 14.18+ and modern browsers)
45
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
46
+ return crypto.randomUUID();
47
+ }
48
+ // Fallback for Node.js: use the crypto module
43
49
  if (RUNTIME.type === "node") {
44
50
  try {
45
51
  // eslint-disable-next-line @typescript-eslint/no-require-imports
46
- const crypto = require("crypto");
47
- return crypto.randomUUID();
52
+ const nodeCrypto = require("crypto");
53
+ return nodeCrypto.randomUUID();
48
54
  }
49
55
  catch (_a) {
50
56
  // Fallback if crypto module is not available
51
57
  }
52
58
  }
53
- // In browser or as fallback, use global crypto if available
54
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
55
- return crypto.randomUUID();
56
- }
57
- // Fallback UUID generation (RFC4122 version 4)
58
- // This is a simple fallback that should work everywhere
59
+ // Final fallback: manual UUID generation (RFC4122 version 4)
60
+ // This should work everywhere but is less secure than crypto.randomUUID()
59
61
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
60
62
  const r = (Math.random() * 16) | 0;
61
63
  const v = c === "x" ? r : (r & 0x3) | 0x8;
62
64
  return v.toString(16);
63
65
  });
64
66
  }
67
+ /**
68
+ * Wrapper auth provider that adds "Token " prefix to API keys.
69
+ * The auto-generated HeaderAuthProvider doesn't add the prefix, so we wrap it here.
70
+ */
71
+ class ApiKeyAuthProviderWrapper {
72
+ constructor(originalProvider) {
73
+ this.originalProvider = originalProvider;
74
+ }
75
+ getAuthRequest(arg) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ var _a, _b;
78
+ const authRequest = yield this.originalProvider.getAuthRequest(arg);
79
+ const authHeader = ((_a = authRequest.headers) === null || _a === void 0 ? void 0 : _a.Authorization) || ((_b = authRequest.headers) === null || _b === void 0 ? void 0 : _b.authorization);
80
+ // If the header doesn't already have a scheme prefix, add "Token " prefix for API keys
81
+ if (authHeader && typeof authHeader === "string") {
82
+ // Only add prefix if it doesn't already have Bearer or Token prefix
83
+ if (!authHeader.startsWith("Bearer ") && !authHeader.startsWith("Token ") && !authHeader.startsWith("token ")) {
84
+ return {
85
+ headers: Object.assign(Object.assign({}, authRequest.headers), { Authorization: `Token ${authHeader}` }),
86
+ };
87
+ }
88
+ }
89
+ return authRequest;
90
+ });
91
+ }
92
+ }
65
93
  /**
66
94
  * Wrapper auth provider that checks for accessToken first (Bearer scheme)
67
95
  * before falling back to the original auth provider (Token scheme for API keys).
@@ -99,7 +127,10 @@ export class CustomDeepgramClient extends DeepgramClient {
99
127
  const optionsWithSessionId = Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, options.headers), { "x-deepgram-session-id": sessionId }) });
100
128
  super(optionsWithSessionId);
101
129
  this._sessionId = sessionId;
102
- // Wrap the auth provider to handle accessToken if provided
130
+ // Always wrap the auth provider to add "Token " prefix to API keys
131
+ // The auto-generated HeaderAuthProvider doesn't add the prefix
132
+ this._options.authProvider = new ApiKeyAuthProviderWrapper(this._options.authProvider);
133
+ // Wrap again to handle accessToken if provided
103
134
  // This ensures accessToken takes priority over apiKey/env var
104
135
  if (options.accessToken != null) {
105
136
  this._options.authProvider = new AccessTokenAuthProviderWrapper(this._options.authProvider, options.accessToken);
@@ -147,6 +178,11 @@ export class CustomDeepgramClient extends DeepgramClient {
147
178
  }
148
179
  /**
149
180
  * Wrapper for AgentClient that ensures custom websocket implementation is used.
181
+ *
182
+ * This wrapper exists to guarantee that our custom WebSocket implementation
183
+ * (from ws.ts) continues to be used even after the SDK code is auto-generated
184
+ * by Fern. Without this wrapper, Fern regeneration could overwrite the client
185
+ * with a different WebSocket implementation.
150
186
  */
151
187
  class WrappedAgentClient extends AgentClientImpl {
152
188
  get v1() {
@@ -155,6 +191,11 @@ class WrappedAgentClient extends AgentClientImpl {
155
191
  }
156
192
  /**
157
193
  * Wrapper for ListenClient that ensures custom websocket implementation is used.
194
+ *
195
+ * This wrapper exists to guarantee that our custom WebSocket implementation
196
+ * (from ws.ts) continues to be used even after the SDK code is auto-generated
197
+ * by Fern. Without this wrapper, Fern regeneration could overwrite the client
198
+ * with a different WebSocket implementation.
158
199
  */
159
200
  class WrappedListenClient extends ListenClientImpl {
160
201
  get v1() {
@@ -166,6 +207,11 @@ class WrappedListenClient extends ListenClientImpl {
166
207
  }
167
208
  /**
168
209
  * Wrapper for SpeakClient that ensures custom websocket implementation is used.
210
+ *
211
+ * This wrapper exists to guarantee that our custom WebSocket implementation
212
+ * (from ws.ts) continues to be used even after the SDK code is auto-generated
213
+ * by Fern. Without this wrapper, Fern regeneration could overwrite the client
214
+ * with a different WebSocket implementation.
169
215
  */
170
216
  class WrappedSpeakClient extends SpeakClientImpl {
171
217
  get v1() {
@@ -255,35 +301,108 @@ function getWebSocketOptions(headers) {
255
301
  }
256
302
  return options;
257
303
  }
304
+ /**
305
+ * Helper function to setup binary-aware message handling for WebSocket sockets.
306
+ * Handles both text (JSON) and binary messages correctly.
307
+ */
308
+ function setupBinaryHandling(socket, eventHandlers) {
309
+ var _a;
310
+ const binaryAwareHandler = (event) => {
311
+ var _a, _b, _c;
312
+ // Handle both text (JSON) and binary messages
313
+ if (typeof event.data === "string") {
314
+ try {
315
+ const data = fromJson(event.data);
316
+ (_a = eventHandlers.message) === null || _a === void 0 ? void 0 : _a.call(eventHandlers, data);
317
+ }
318
+ catch (error) {
319
+ // If JSON parsing fails, pass the raw string
320
+ (_b = eventHandlers.message) === null || _b === void 0 ? void 0 : _b.call(eventHandlers, event.data);
321
+ }
322
+ }
323
+ else {
324
+ // Binary data - pass through as-is
325
+ (_c = eventHandlers.message) === null || _c === void 0 ? void 0 : _c.call(eventHandlers, event.data);
326
+ }
327
+ };
328
+ // Remove the original handler and add our binary-aware one
329
+ const socketAny = socket;
330
+ if ((_a = socketAny._listeners) === null || _a === void 0 ? void 0 : _a.message) {
331
+ // Remove all message listeners
332
+ socketAny._listeners.message.forEach((listener) => {
333
+ socket.removeEventListener("message", listener);
334
+ });
335
+ }
336
+ // Add our binary-aware handler
337
+ socket.addEventListener("message", binaryAwareHandler);
338
+ return binaryAwareHandler;
339
+ }
340
+ /**
341
+ * Helper function to reset socket connection state before connecting.
342
+ * Ensures _connectLock is reset if the socket is in CLOSED state.
343
+ */
344
+ function resetSocketConnectionState(socket) {
345
+ if (socket.readyState === socket.CLOSED) {
346
+ // Force a fresh reconnect to ensure _connectLock is reset
347
+ socket._connectLock = false;
348
+ socket._shouldReconnect = true;
349
+ }
350
+ }
351
+ /**
352
+ * Helper function to create a WebSocket connection with common setup logic.
353
+ * Handles authentication, header merging, and WebSocket configuration.
354
+ * This reduces duplication across all Wrapped*Client classes.
355
+ */
356
+ function createWebSocketConnection(_a) {
357
+ return __awaiter(this, arguments, void 0, function* ({ options, urlPath, environmentKey, queryParams, headers, debug, reconnectAttempts, }) {
358
+ var _b, _c, _d, _e, _f, _g;
359
+ // Get Authorization from authProvider (cast to any to access internal property)
360
+ const authRequest = yield ((_b = options.authProvider) === null || _b === void 0 ? void 0 : _b.getAuthRequest());
361
+ // Merge headers from options (which includes session ID), auth headers, and request headers
362
+ const mergedHeaders = mergeHeaders((_c = options.headers) !== null && _c !== void 0 ? _c : {}, (_d = authRequest === null || authRequest === void 0 ? void 0 : authRequest.headers) !== null && _d !== void 0 ? _d : {}, headers);
363
+ // Resolve any Suppliers in headers to actual values
364
+ const _headers = yield resolveHeaders(mergedHeaders);
365
+ // Get WebSocket options with proper header handling
366
+ const wsOptions = getWebSocketOptions(_headers);
367
+ // Get the appropriate base URL for the environment
368
+ const baseUrl = (_e = (yield core.Supplier.get(options.baseUrl))) !== null && _e !== void 0 ? _e : ((_f = (yield core.Supplier.get(options.environment))) !== null && _f !== void 0 ? _f : environments.DeepgramEnvironment.Production)[environmentKey];
369
+ // Create and return the ReconnectingWebSocket
370
+ return new ReconnectingWebSocket({
371
+ url: core.url.join(baseUrl, urlPath),
372
+ protocols: (_g = wsOptions.protocols) !== null && _g !== void 0 ? _g : [],
373
+ queryParameters: queryParams,
374
+ headers: wsOptions.headers,
375
+ options: {
376
+ WebSocket: wsOptions.WebSocket,
377
+ debug: debug !== null && debug !== void 0 ? debug : false,
378
+ maxRetries: reconnectAttempts !== null && reconnectAttempts !== void 0 ? reconnectAttempts : 30,
379
+ startClosed: true,
380
+ connectionTimeout: DEFAULT_CONNECTION_TIMEOUT_MS,
381
+ },
382
+ });
383
+ });
384
+ }
258
385
  /**
259
386
  * Wrapper for Agent V1Client that overrides connect to use custom websocket.
387
+ *
388
+ * This wrapper ensures that the connect() method uses our custom ReconnectingWebSocket
389
+ * implementation instead of any auto-generated WebSocket handling. This guarantees
390
+ * consistent behavior across Fern regenerations and allows us to customize
391
+ * connection setup, authentication, and header handling.
260
392
  */
261
393
  class WrappedAgentV1Client extends AgentV1Client {
262
394
  connect() {
263
395
  return __awaiter(this, arguments, void 0, function* (args = {}) {
264
- var _a, _b, _c, _d, _e, _f;
265
396
  const { headers, debug, reconnectAttempts } = args;
266
- // Get Authorization from authProvider (matching the working version)
267
- const authRequest = yield ((_a = this._options.authProvider) === null || _a === void 0 ? void 0 : _a.getAuthRequest());
268
- // Merge headers from options (which includes session ID), auth headers, and request headers
269
- const mergedHeaders = mergeHeaders((_b = this._options.headers) !== null && _b !== void 0 ? _b : {}, (_c = authRequest === null || authRequest === void 0 ? void 0 : authRequest.headers) !== null && _c !== void 0 ? _c : {}, headers);
270
- // Resolve any Suppliers in headers to actual values
271
- const _headers = yield resolveHeaders(mergedHeaders);
272
- // Get WebSocket options with proper header handling
273
- const wsOptions = getWebSocketOptions(_headers);
274
- // Explicitly use the custom ReconnectingWebSocket from ws.ts
275
- const socket = new ReconnectingWebSocket({
276
- url: core.url.join((_d = (yield core.Supplier.get(this._options.baseUrl))) !== null && _d !== void 0 ? _d : ((_e = (yield core.Supplier.get(this._options.environment))) !== null && _e !== void 0 ? _e : environments.DeepgramEnvironment.Production).agent, "/v1/agent/converse"),
277
- protocols: (_f = wsOptions.protocols) !== null && _f !== void 0 ? _f : [],
278
- queryParameters: {},
279
- headers: wsOptions.headers,
280
- options: {
281
- WebSocket: wsOptions.WebSocket,
282
- debug: debug !== null && debug !== void 0 ? debug : false,
283
- maxRetries: reconnectAttempts !== null && reconnectAttempts !== void 0 ? reconnectAttempts : 30,
284
- startClosed: true,
285
- connectionTimeout: 10000, // Increase timeout to 10 seconds
286
- },
397
+ // Use shared connection helper
398
+ const socket = yield createWebSocketConnection({
399
+ options: this._options,
400
+ urlPath: "/v1/agent/converse",
401
+ environmentKey: 'agent',
402
+ queryParams: {},
403
+ headers,
404
+ debug,
405
+ reconnectAttempts,
287
406
  });
288
407
  return new WrappedAgentV1Socket({ socket });
289
408
  });
@@ -291,6 +410,11 @@ class WrappedAgentV1Client extends AgentV1Client {
291
410
  }
292
411
  /**
293
412
  * Wrapper for Agent V1Socket that handles binary messages correctly.
413
+ *
414
+ * This wrapper ensures that both text (JSON) and binary WebSocket messages are
415
+ * handled properly. The auto-generated socket class may not handle binary data
416
+ * correctly, so we override the message handling to support both formats.
417
+ * This is critical for audio and other binary data streaming.
294
418
  */
295
419
  class WrappedAgentV1Socket extends AgentV1Socket {
296
420
  constructor(args) {
@@ -298,50 +422,10 @@ class WrappedAgentV1Socket extends AgentV1Socket {
298
422
  this.setupBinaryHandling();
299
423
  }
300
424
  setupBinaryHandling() {
301
- var _a;
302
- // Create a binary-aware message handler
303
- this.binaryAwareHandler = (event) => {
304
- var _a, _b, _c, _d, _e, _f;
305
- // Handle both text (JSON) and binary messages
306
- if (typeof event.data === "string") {
307
- try {
308
- const data = fromJson(event.data);
309
- (_b = (_a = this.eventHandlers).message) === null || _b === void 0 ? void 0 : _b.call(_a, data);
310
- }
311
- catch (error) {
312
- // If JSON parsing fails, pass the raw string
313
- (_d = (_c = this.eventHandlers).message) === null || _d === void 0 ? void 0 : _d.call(_c, event.data);
314
- }
315
- }
316
- else {
317
- // Binary data - pass through as-is
318
- (_f = (_e = this.eventHandlers).message) === null || _f === void 0 ? void 0 : _f.call(_e, event.data);
319
- }
320
- };
321
- // Remove the original handler and add our binary-aware one
322
- // The original handler was added in super() constructor
323
- // We need to replace it
324
- const socketAny = this.socket;
325
- if ((_a = socketAny._listeners) === null || _a === void 0 ? void 0 : _a.message) {
326
- // Remove all message listeners
327
- socketAny._listeners.message.forEach((listener) => {
328
- this.socket.removeEventListener("message", listener);
329
- });
330
- }
331
- // Add our binary-aware handler
332
- if (this.binaryAwareHandler) {
333
- this.socket.addEventListener("message", this.binaryAwareHandler);
334
- }
425
+ this.binaryAwareHandler = setupBinaryHandling(this.socket, this.eventHandlers);
335
426
  }
336
427
  connect() {
337
- // Ensure socket is ready to connect - if _connectLock is stuck, force reconnect
338
- // by closing and reconnecting
339
- if (this.socket.readyState === this.socket.CLOSED) {
340
- // Force a fresh reconnect to ensure _connectLock is reset
341
- this.socket._connectLock = false;
342
- this.socket._shouldReconnect = true;
343
- }
344
- // Call parent connect, but then ensure our binary handler is still active
428
+ resetSocketConnectionState(this.socket);
345
429
  super.connect();
346
430
  // Re-setup binary handling in case connect() re-registered handlers
347
431
  this.setupBinaryHandling();
@@ -350,11 +434,15 @@ class WrappedAgentV1Socket extends AgentV1Socket {
350
434
  }
351
435
  /**
352
436
  * Wrapper for Listen V1Client that overrides connect to use custom websocket.
437
+ *
438
+ * This wrapper ensures that the connect() method uses our custom ReconnectingWebSocket
439
+ * implementation instead of any auto-generated WebSocket handling. This guarantees
440
+ * consistent behavior across Fern regenerations and allows us to customize
441
+ * connection setup, authentication, and header handling.
353
442
  */
354
443
  class WrappedListenV1Client extends ListenV1Client {
355
444
  connect(args) {
356
445
  return __awaiter(this, void 0, void 0, function* () {
357
- var _a, _b, _c, _d;
358
446
  // Extract all the args (same as the original implementation)
359
447
  const { callback, callback_method: callbackMethod, channels, diarize, dictation, encoding, endpointing, extra, interim_results: interimResults, keyterm, keywords, language, mip_opt_out: mipOptOut, model, multichannel, numerals, profanity_filter: profanityFilter, punctuate, redact, replace, sample_rate: sampleRate, search, smart_format: smartFormat, tag, utterance_end_ms: utteranceEndMs, vad_events: vadEvents, version, headers, debug, reconnectAttempts, } = args;
360
448
  // Build query params (same as original)
@@ -412,27 +500,15 @@ class WrappedListenV1Client extends ListenV1Client {
412
500
  _queryParams.vad_events = vadEvents;
413
501
  if (version != null)
414
502
  _queryParams.version = version;
415
- // Get Authorization from authProvider (matching the working version)
416
- const authRequest = yield this._options.authProvider.getAuthRequest();
417
- // Merge headers from options (which includes session ID), auth headers, and request headers
418
- const mergedHeaders = mergeHeaders((_a = this._options.headers) !== null && _a !== void 0 ? _a : {}, authRequest.headers, headers);
419
- // Resolve any Suppliers in headers to actual values
420
- const _headers = yield resolveHeaders(mergedHeaders);
421
- // Get WebSocket options with proper header handling
422
- const wsOptions = getWebSocketOptions(_headers);
423
- // Explicitly use the custom ReconnectingWebSocket from ws.ts
424
- const socket = new ReconnectingWebSocket({
425
- url: core.url.join((_b = (yield core.Supplier.get(this._options.baseUrl))) !== null && _b !== void 0 ? _b : ((_c = (yield core.Supplier.get(this._options.environment))) !== null && _c !== void 0 ? _c : environments.DeepgramEnvironment.Production).production, "/v1/listen"),
426
- protocols: (_d = wsOptions.protocols) !== null && _d !== void 0 ? _d : [],
427
- queryParameters: _queryParams,
428
- headers: wsOptions.headers,
429
- options: {
430
- WebSocket: wsOptions.WebSocket,
431
- debug: debug !== null && debug !== void 0 ? debug : false,
432
- maxRetries: reconnectAttempts !== null && reconnectAttempts !== void 0 ? reconnectAttempts : 30,
433
- startClosed: true,
434
- connectionTimeout: 10000, // Increase timeout to 10 seconds
435
- },
503
+ // Use shared connection helper
504
+ const socket = yield createWebSocketConnection({
505
+ options: this._options,
506
+ urlPath: "/v1/listen",
507
+ environmentKey: 'production',
508
+ queryParams: _queryParams,
509
+ headers,
510
+ debug,
511
+ reconnectAttempts,
436
512
  });
437
513
  return new WrappedListenV1Socket({ socket });
438
514
  });
@@ -440,6 +516,11 @@ class WrappedListenV1Client extends ListenV1Client {
440
516
  }
441
517
  /**
442
518
  * Wrapper for Listen V1Socket that handles binary messages correctly.
519
+ *
520
+ * This wrapper ensures that both text (JSON) and binary WebSocket messages are
521
+ * handled properly. The auto-generated socket class may not handle binary data
522
+ * correctly, so we override the message handling to support both formats.
523
+ * This is critical for audio and other binary data streaming.
443
524
  */
444
525
  class WrappedListenV1Socket extends ListenV1Socket {
445
526
  constructor(args) {
@@ -447,44 +528,10 @@ class WrappedListenV1Socket extends ListenV1Socket {
447
528
  this.setupBinaryHandling();
448
529
  }
449
530
  setupBinaryHandling() {
450
- var _a;
451
- // Create a binary-aware message handler
452
- this.binaryAwareHandler = (event) => {
453
- var _a, _b, _c, _d, _e, _f;
454
- // Handle both text (JSON) and binary messages
455
- if (typeof event.data === "string") {
456
- try {
457
- const data = fromJson(event.data);
458
- (_b = (_a = this.eventHandlers).message) === null || _b === void 0 ? void 0 : _b.call(_a, data);
459
- }
460
- catch (error) {
461
- // If JSON parsing fails, pass the raw string
462
- (_d = (_c = this.eventHandlers).message) === null || _d === void 0 ? void 0 : _d.call(_c, event.data);
463
- }
464
- }
465
- else {
466
- // Binary data - pass through as-is
467
- (_f = (_e = this.eventHandlers).message) === null || _f === void 0 ? void 0 : _f.call(_e, event.data);
468
- }
469
- };
470
- // Remove the original handler and add our binary-aware one
471
- const socketAny = this.socket;
472
- if ((_a = socketAny._listeners) === null || _a === void 0 ? void 0 : _a.message) {
473
- socketAny._listeners.message.forEach((listener) => {
474
- this.socket.removeEventListener("message", listener);
475
- });
476
- }
477
- if (this.binaryAwareHandler) {
478
- this.socket.addEventListener("message", this.binaryAwareHandler);
479
- }
531
+ this.binaryAwareHandler = setupBinaryHandling(this.socket, this.eventHandlers);
480
532
  }
481
533
  connect() {
482
- // Ensure socket is ready to connect - if _connectLock is stuck, force reconnect
483
- if (this.socket.readyState === this.socket.CLOSED) {
484
- // Force a fresh reconnect to ensure _connectLock is reset
485
- this.socket._connectLock = false;
486
- this.socket._shouldReconnect = true;
487
- }
534
+ resetSocketConnectionState(this.socket);
488
535
  super.connect();
489
536
  this.setupBinaryHandling();
490
537
  return this;
@@ -492,11 +539,15 @@ class WrappedListenV1Socket extends ListenV1Socket {
492
539
  }
493
540
  /**
494
541
  * Wrapper for Listen V2Client that overrides connect to use custom websocket.
542
+ *
543
+ * This wrapper ensures that the connect() method uses our custom ReconnectingWebSocket
544
+ * implementation instead of any auto-generated WebSocket handling. This guarantees
545
+ * consistent behavior across Fern regenerations and allows us to customize
546
+ * connection setup, authentication, and header handling.
495
547
  */
496
548
  class WrappedListenV2Client extends ListenV2Client {
497
549
  connect(args) {
498
550
  return __awaiter(this, void 0, void 0, function* () {
499
- var _a, _b, _c, _d, _e, _f;
500
551
  const { model, encoding, sample_rate: sampleRate, eager_eot_threshold: eagerEotThreshold, eot_threshold: eotThreshold, eot_timeout_ms: eotTimeoutMs, keyterm, mip_opt_out: mipOptOut, tag, headers, debug, reconnectAttempts, } = args;
501
552
  const _queryParams = {};
502
553
  _queryParams.model = model;
@@ -516,34 +567,28 @@ class WrappedListenV2Client extends ListenV2Client {
516
567
  _queryParams.mip_opt_out = mipOptOut;
517
568
  if (tag != null)
518
569
  _queryParams.tag = tag;
519
- // Get Authorization from authProvider (matching the working version)
520
- const authRequest = yield ((_a = this._options.authProvider) === null || _a === void 0 ? void 0 : _a.getAuthRequest());
521
- // Merge headers from options (which includes session ID), auth headers, and request headers
522
- const mergedHeaders = mergeHeaders((_b = this._options.headers) !== null && _b !== void 0 ? _b : {}, (_c = authRequest === null || authRequest === void 0 ? void 0 : authRequest.headers) !== null && _c !== void 0 ? _c : {}, headers);
523
- // Resolve any Suppliers in headers to actual values
524
- const _headers = yield resolveHeaders(mergedHeaders);
525
- // Get WebSocket options with proper header handling
526
- const wsOptions = getWebSocketOptions(_headers);
527
- // Explicitly use the custom ReconnectingWebSocket from ws.ts
528
- const socket = new ReconnectingWebSocket({
529
- url: core.url.join((_d = (yield core.Supplier.get(this._options.baseUrl))) !== null && _d !== void 0 ? _d : ((_e = (yield core.Supplier.get(this._options.environment))) !== null && _e !== void 0 ? _e : environments.DeepgramEnvironment.Production).production, "/v2/listen"),
530
- protocols: (_f = wsOptions.protocols) !== null && _f !== void 0 ? _f : [],
531
- queryParameters: _queryParams,
532
- headers: wsOptions.headers,
533
- options: {
534
- WebSocket: wsOptions.WebSocket,
535
- debug: debug !== null && debug !== void 0 ? debug : false,
536
- maxRetries: reconnectAttempts !== null && reconnectAttempts !== void 0 ? reconnectAttempts : 30,
537
- startClosed: true,
538
- connectionTimeout: 10000, // Increase timeout to 10 seconds
539
- },
570
+ // Use shared connection helper
571
+ const socket = yield createWebSocketConnection({
572
+ options: this._options,
573
+ urlPath: "/v2/listen",
574
+ environmentKey: 'production',
575
+ queryParams: _queryParams,
576
+ headers,
577
+ debug,
578
+ reconnectAttempts,
540
579
  });
541
580
  return new WrappedListenV2Socket({ socket });
542
581
  });
543
582
  }
544
583
  }
545
584
  /**
546
- * Wrapper for Listen V2Socket that handles binary messages correctly.
585
+ * Wrapper for Listen V2Socket that handles binary messages correctly and adds ping support.
586
+ *
587
+ * This wrapper ensures that both text (JSON) and binary WebSocket messages are
588
+ * handled properly. The auto-generated socket class may not handle binary data
589
+ * correctly, so we override the message handling to support both formats.
590
+ * Additionally, this wrapper adds a ping() method for sending WebSocket ping
591
+ * frames to keep connections alive (Node.js only).
547
592
  */
548
593
  class WrappedListenV2Socket extends ListenV2Socket {
549
594
  constructor(args) {
@@ -551,56 +596,56 @@ class WrappedListenV2Socket extends ListenV2Socket {
551
596
  this.setupBinaryHandling();
552
597
  }
553
598
  setupBinaryHandling() {
554
- var _a;
555
- // Create a binary-aware message handler
556
- this.binaryAwareHandler = (event) => {
557
- var _a, _b, _c, _d, _e, _f;
558
- // Handle both text (JSON) and binary messages
559
- if (typeof event.data === "string") {
560
- try {
561
- const data = fromJson(event.data);
562
- (_b = (_a = this.eventHandlers).message) === null || _b === void 0 ? void 0 : _b.call(_a, data);
563
- }
564
- catch (error) {
565
- // If JSON parsing fails, pass the raw string
566
- (_d = (_c = this.eventHandlers).message) === null || _d === void 0 ? void 0 : _d.call(_c, event.data);
567
- }
568
- }
569
- else {
570
- // Binary data - pass through as-is
571
- (_f = (_e = this.eventHandlers).message) === null || _f === void 0 ? void 0 : _f.call(_e, event.data);
572
- }
573
- };
574
- // Remove the original handler and add our binary-aware one
575
- const socketAny = this.socket;
576
- if ((_a = socketAny._listeners) === null || _a === void 0 ? void 0 : _a.message) {
577
- socketAny._listeners.message.forEach((listener) => {
578
- this.socket.removeEventListener("message", listener);
579
- });
580
- }
581
- if (this.binaryAwareHandler) {
582
- this.socket.addEventListener("message", this.binaryAwareHandler);
583
- }
599
+ this.binaryAwareHandler = setupBinaryHandling(this.socket, this.eventHandlers);
584
600
  }
585
601
  connect() {
586
- // Ensure socket is ready to connect - if _connectLock is stuck, force reconnect
587
- if (this.socket.readyState === this.socket.CLOSED) {
588
- // Force a fresh reconnect to ensure _connectLock is reset
589
- this.socket._connectLock = false;
590
- this.socket._shouldReconnect = true;
591
- }
602
+ resetSocketConnectionState(this.socket);
592
603
  super.connect();
593
604
  this.setupBinaryHandling();
594
605
  return this;
595
606
  }
607
+ /**
608
+ * Send a WebSocket ping frame to keep the connection alive.
609
+ *
610
+ * In Node.js, this uses the native WebSocket ping() method from the 'ws' library.
611
+ * In browsers, WebSocket ping/pong is handled automatically by the browser and
612
+ * cannot be manually triggered, so this method will throw an error.
613
+ *
614
+ * @param data Optional data to send with the ping (Node.js only)
615
+ * @throws Error if not in Node.js environment or WebSocket is not connected
616
+ */
617
+ ping(data) {
618
+ const ws = this.socket._ws;
619
+ if (!ws) {
620
+ throw new Error("WebSocket is not connected. Call connect() and waitForOpen() first.");
621
+ }
622
+ if (ws.readyState !== ws.OPEN) {
623
+ throw new Error("WebSocket is not in OPEN state.");
624
+ }
625
+ // Check if we're in Node.js and the WebSocket has a ping method (from 'ws' library)
626
+ if (RUNTIME.type === "node" && typeof ws.ping === "function") {
627
+ // Call the native ping method from the 'ws' library
628
+ ws.ping(data);
629
+ }
630
+ else {
631
+ // In browsers, WebSocket ping/pong is automatic and not exposed to JavaScript
632
+ throw new Error("WebSocket ping is not supported in browser environments. " +
633
+ "Browser WebSocket connections handle ping/pong automatically. " +
634
+ "If you need keepalive in the browser, consider sending periodic audio data or using a timer.");
635
+ }
636
+ }
596
637
  }
597
638
  /**
598
639
  * Wrapper for Speak V1Client that overrides connect to use custom websocket.
640
+ *
641
+ * This wrapper ensures that the connect() method uses our custom ReconnectingWebSocket
642
+ * implementation instead of any auto-generated WebSocket handling. This guarantees
643
+ * consistent behavior across Fern regenerations and allows us to customize
644
+ * connection setup, authentication, and header handling.
599
645
  */
600
646
  class WrappedSpeakV1Client extends SpeakV1Client {
601
647
  connect(args) {
602
648
  return __awaiter(this, void 0, void 0, function* () {
603
- var _a, _b, _c, _d;
604
649
  const { encoding, mip_opt_out: mipOptOut, model, sample_rate: sampleRate, headers, debug, reconnectAttempts, } = args;
605
650
  const _queryParams = {};
606
651
  if (encoding != null)
@@ -611,27 +656,15 @@ class WrappedSpeakV1Client extends SpeakV1Client {
611
656
  _queryParams.model = model;
612
657
  if (sampleRate != null)
613
658
  _queryParams.sample_rate = sampleRate;
614
- // Get Authorization from authProvider (matching the working version)
615
- const authRequest = yield this._options.authProvider.getAuthRequest();
616
- // Merge headers from options (which includes session ID), auth headers, and request headers
617
- const mergedHeaders = mergeHeaders((_a = this._options.headers) !== null && _a !== void 0 ? _a : {}, authRequest.headers, headers);
618
- // Resolve any Suppliers in headers to actual values
619
- const _headers = yield resolveHeaders(mergedHeaders);
620
- // Get WebSocket options with proper header handling
621
- const wsOptions = getWebSocketOptions(_headers);
622
- // Explicitly use the custom ReconnectingWebSocket from ws.ts
623
- const socket = new ReconnectingWebSocket({
624
- url: core.url.join((_b = (yield core.Supplier.get(this._options.baseUrl))) !== null && _b !== void 0 ? _b : ((_c = (yield core.Supplier.get(this._options.environment))) !== null && _c !== void 0 ? _c : environments.DeepgramEnvironment.Production).production, "/v1/speak"),
625
- protocols: (_d = wsOptions.protocols) !== null && _d !== void 0 ? _d : [],
626
- queryParameters: _queryParams,
627
- headers: wsOptions.headers,
628
- options: {
629
- WebSocket: wsOptions.WebSocket,
630
- debug: debug !== null && debug !== void 0 ? debug : false,
631
- maxRetries: reconnectAttempts !== null && reconnectAttempts !== void 0 ? reconnectAttempts : 30,
632
- startClosed: true,
633
- connectionTimeout: 10000, // Increase timeout to 10 seconds
634
- },
659
+ // Use shared connection helper
660
+ const socket = yield createWebSocketConnection({
661
+ options: this._options,
662
+ urlPath: "/v1/speak",
663
+ environmentKey: 'production',
664
+ queryParams: _queryParams,
665
+ headers,
666
+ debug,
667
+ reconnectAttempts,
635
668
  });
636
669
  return new WrappedSpeakV1Socket({ socket });
637
670
  });
@@ -639,6 +672,11 @@ class WrappedSpeakV1Client extends SpeakV1Client {
639
672
  }
640
673
  /**
641
674
  * Wrapper for Speak V1Socket that handles binary messages correctly.
675
+ *
676
+ * This wrapper ensures that both text (JSON) and binary WebSocket messages are
677
+ * handled properly. The auto-generated socket class may not handle binary data
678
+ * correctly, so we override the message handling to support both formats.
679
+ * This is critical for audio and other binary data streaming.
642
680
  */
643
681
  class WrappedSpeakV1Socket extends SpeakV1Socket {
644
682
  constructor(args) {
@@ -646,44 +684,10 @@ class WrappedSpeakV1Socket extends SpeakV1Socket {
646
684
  this.setupBinaryHandling();
647
685
  }
648
686
  setupBinaryHandling() {
649
- var _a;
650
- // Create a binary-aware message handler
651
- this.binaryAwareHandler = (event) => {
652
- var _a, _b, _c, _d, _e, _f;
653
- // Handle both text (JSON) and binary messages
654
- if (typeof event.data === "string") {
655
- try {
656
- const data = fromJson(event.data);
657
- (_b = (_a = this.eventHandlers).message) === null || _b === void 0 ? void 0 : _b.call(_a, data);
658
- }
659
- catch (error) {
660
- // If JSON parsing fails, pass the raw string
661
- (_d = (_c = this.eventHandlers).message) === null || _d === void 0 ? void 0 : _d.call(_c, event.data);
662
- }
663
- }
664
- else {
665
- // Binary data - pass through as-is
666
- (_f = (_e = this.eventHandlers).message) === null || _f === void 0 ? void 0 : _f.call(_e, event.data);
667
- }
668
- };
669
- // Remove the original handler and add our binary-aware one
670
- const socketAny = this.socket;
671
- if ((_a = socketAny._listeners) === null || _a === void 0 ? void 0 : _a.message) {
672
- socketAny._listeners.message.forEach((listener) => {
673
- this.socket.removeEventListener("message", listener);
674
- });
675
- }
676
- if (this.binaryAwareHandler) {
677
- this.socket.addEventListener("message", this.binaryAwareHandler);
678
- }
687
+ this.binaryAwareHandler = setupBinaryHandling(this.socket, this.eventHandlers);
679
688
  }
680
689
  connect() {
681
- // Ensure socket is ready to connect - if _connectLock is stuck, force reconnect
682
- if (this.socket.readyState === this.socket.CLOSED) {
683
- // Force a fresh reconnect to ensure _connectLock is reset
684
- this.socket._connectLock = false;
685
- this.socket._shouldReconnect = true;
686
- }
690
+ resetSocketConnectionState(this.socket);
687
691
  super.connect();
688
692
  this.setupBinaryHandling();
689
693
  return this;