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