@corti/dictation-web 0.5.1 → 0.5.2-rc.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.
- package/dist/bundle.js +605 -131
- package/dist/components/recording-button.js +12 -16
- package/dist/components/recording-button.js.map +1 -1
- package/dist/controllers/dictation-controller.d.ts +4 -1
- package/dist/controllers/dictation-controller.js +133 -33
- package/dist/controllers/dictation-controller.js.map +1 -1
- package/dist/controllers/media-controller.d.ts +1 -3
- package/dist/controllers/media-controller.js +4 -22
- package/dist/controllers/media-controller.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/events.js +3 -1
- package/dist/utils/events.js.map +1 -1
- package/package.json +2 -2
|
@@ -43,13 +43,6 @@ let DictationRecordingButton = class DictationRecordingButton extends LitElement
|
|
|
43
43
|
_DictationRecordingButton_connection.set(this, "CLOSED");
|
|
44
44
|
_DictationRecordingButton_handleWebSocketMessage.set(this, (message) => {
|
|
45
45
|
switch (message.type) {
|
|
46
|
-
case "CONFIG_ACCEPTED":
|
|
47
|
-
__classPrivateFieldGet(this, _DictationRecordingButton_mediaController, "f").addDataHandler(__classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").mediaRecorderHandler);
|
|
48
|
-
if (this._recordingState === "initializing") {
|
|
49
|
-
__classPrivateFieldSet(this, _DictationRecordingButton_processing, true, "f");
|
|
50
|
-
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this, "recording");
|
|
51
|
-
}
|
|
52
|
-
break;
|
|
53
46
|
case "CONFIG_DENIED":
|
|
54
47
|
this.dispatchEvent(errorEvent(`Config denied: ${message.reason ?? "Unknown reason"}`));
|
|
55
48
|
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_handleStop).call(this);
|
|
@@ -88,7 +81,8 @@ let DictationRecordingButton = class DictationRecordingButton extends LitElement
|
|
|
88
81
|
});
|
|
89
82
|
_DictationRecordingButton_handleWebSocketClose.set(this, (event) => {
|
|
90
83
|
// When we already have new socket opened
|
|
91
|
-
if (__classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").isConnectionOpen()
|
|
84
|
+
if (__classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").isConnectionOpen() ||
|
|
85
|
+
__classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").isConnecting()) {
|
|
92
86
|
return;
|
|
93
87
|
}
|
|
94
88
|
__classPrivateFieldSet(this, _DictationRecordingButton_processing, false, "f");
|
|
@@ -140,7 +134,7 @@ let DictationRecordingButton = class DictationRecordingButton extends LitElement
|
|
|
140
134
|
}
|
|
141
135
|
try {
|
|
142
136
|
__classPrivateFieldSet(this, _DictationRecordingButton_connection, "CONNECTING", "f");
|
|
143
|
-
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this,
|
|
137
|
+
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this, this._recordingState);
|
|
144
138
|
await __classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").connect(this._dictationConfig, {
|
|
145
139
|
onClose: __classPrivateFieldGet(this, _DictationRecordingButton_handleWebSocketClose, "f"),
|
|
146
140
|
onError: __classPrivateFieldGet(this, _DictationRecordingButton_handleWebSocketError, "f"),
|
|
@@ -150,7 +144,7 @@ let DictationRecordingButton = class DictationRecordingButton extends LitElement
|
|
|
150
144
|
},
|
|
151
145
|
});
|
|
152
146
|
__classPrivateFieldSet(this, _DictationRecordingButton_connection, "OPEN", "f");
|
|
153
|
-
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this,
|
|
147
|
+
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this, this._recordingState);
|
|
154
148
|
}
|
|
155
149
|
catch (error) {
|
|
156
150
|
__classPrivateFieldSet(this, _DictationRecordingButton_connection, "CLOSED", "f");
|
|
@@ -161,6 +155,9 @@ let DictationRecordingButton = class DictationRecordingButton extends LitElement
|
|
|
161
155
|
if (this._recordingState !== "stopped" || __classPrivateFieldGet(this, _DictationRecordingButton_processing, "f")) {
|
|
162
156
|
return;
|
|
163
157
|
}
|
|
158
|
+
if (__classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").isConnecting()) {
|
|
159
|
+
await __classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").waitForConnection();
|
|
160
|
+
}
|
|
164
161
|
if (!__classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").isConnectionOpen()) {
|
|
165
162
|
__classPrivateFieldSet(this, _DictationRecordingButton_connection, "CLOSED", "f");
|
|
166
163
|
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this, "stopped");
|
|
@@ -230,11 +227,12 @@ _DictationRecordingButton_handleStart = async function _DictationRecordingButton
|
|
|
230
227
|
this.dispatchEvent(errorEvent("Recording device access was lost."));
|
|
231
228
|
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_handleStop).call(this);
|
|
232
229
|
}
|
|
233
|
-
});
|
|
230
|
+
}, __classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").mediaRecorderHandler);
|
|
234
231
|
__classPrivateFieldGet(this, _DictationRecordingButton_mediaController, "f").mediaRecorder?.start(AUDIO_CHUNK_INTERVAL_MS);
|
|
235
232
|
__classPrivateFieldGet(this, _DictationRecordingButton_mediaController, "f").startAudioLevelMonitoring((level) => {
|
|
236
233
|
this.dispatchEvent(audioLevelChangedEvent(level));
|
|
237
234
|
});
|
|
235
|
+
__classPrivateFieldSet(this, _DictationRecordingButton_processing, true, "f");
|
|
238
236
|
if (__classPrivateFieldGet(this, _DictationRecordingButton_connection, "f") !== "OPEN") {
|
|
239
237
|
__classPrivateFieldSet(this, _DictationRecordingButton_connection, "CONNECTING", "f");
|
|
240
238
|
}
|
|
@@ -247,12 +245,11 @@ _DictationRecordingButton_handleStart = async function _DictationRecordingButton
|
|
|
247
245
|
this.dispatchEvent(networkActivityEvent(direction, data));
|
|
248
246
|
},
|
|
249
247
|
});
|
|
250
|
-
if (
|
|
251
|
-
|
|
252
|
-
__classPrivateFieldSet(this, _DictationRecordingButton_processing, true, "f");
|
|
248
|
+
if (isNewConnection === "superseded") {
|
|
249
|
+
return;
|
|
253
250
|
}
|
|
254
251
|
__classPrivateFieldSet(this, _DictationRecordingButton_connection, "OPEN", "f");
|
|
255
|
-
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this,
|
|
252
|
+
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this, this._recordingState);
|
|
256
253
|
}
|
|
257
254
|
catch (error) {
|
|
258
255
|
this.dispatchEvent(errorEvent(error));
|
|
@@ -264,7 +261,6 @@ _DictationRecordingButton_handleStop = async function _DictationRecordingButton_
|
|
|
264
261
|
try {
|
|
265
262
|
__classPrivateFieldGet(this, _DictationRecordingButton_mediaController, "f").stopAudioLevelMonitoring();
|
|
266
263
|
await __classPrivateFieldGet(this, _DictationRecordingButton_mediaController, "f").stopRecording();
|
|
267
|
-
__classPrivateFieldGet(this, _DictationRecordingButton_mediaController, "f").removeDataHandler();
|
|
268
264
|
__classPrivateFieldGet(this, _DictationRecordingButton_instances, "m", _DictationRecordingButton_dispatchRecordingStateChanged).call(this, "stopped");
|
|
269
265
|
await __classPrivateFieldGet(this, _DictationRecordingButton_dictationController, "f").pause();
|
|
270
266
|
await __classPrivateFieldGet(this, _DictationRecordingButton_mediaController, "f").cleanup();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recording-button.js","sourceRoot":"","sources":["../../src/components/recording-button.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;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E,IAAI,EAAE,CAAC;IACxD,KAAK,EAAE;6DAC0B;AAIlC;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IACpD,KAAK,EAAE;yDACS;AAIjB;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IACxD,KAAK,EAAE;6DACa;AAIrB;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAC7D,KAAK,EAAE;kEACkC;AAI1C;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IACvD,KAAK,EAAE;4DACY;AAIpB;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,kBAAkB,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IACzD,KAAK,EAAE;8DACoB;AAI5B;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,wBAAwB,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAC/D,KAAK,EAAE;qEACsB;AAI9B;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,2BAA2B,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IAClE,KAAK,EAAE;uEAC8B;AAItC;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,6BAA6B,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IACpE,KAAK,EAAE;yEACgC;AAGxC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;kEACM;AAlDvB,wBAAwB;IADpC,aAAa,CAAC,4BAA4B,CAAC;GAC/B,wBAAwB,CA+UpC","sourcesContent":["import type { Corti } from \"@corti/sdk\";\nimport { consume } from \"@lit/context\";\nimport {\n type CSSResultGroup,\n html,\n LitElement,\n type PropertyValues,\n} from \"lit\";\nimport { customElement, property, state } from \"lit/decorators.js\";\nimport { AUDIO_CHUNK_INTERVAL_MS } from \"../constants.js\";\nimport {\n accessTokenContext,\n authConfigContext,\n debugDisplayAudioContext,\n dictationConfigContext,\n pushToTalkKeybindingContext,\n recordingStateContext,\n regionContext,\n selectedDeviceContext,\n socketProxyContext,\n socketUrlContext,\n tenantNameContext,\n toggleToTalkKeybindingContext,\n} from \"../contexts/dictation-context.js\";\nimport {\n DictationController,\n type TranscribeMessage,\n} from \"../controllers/dictation-controller.js\";\nimport { KeybindingController } from \"../controllers/keybinding-controller.js\";\nimport { MediaController } from \"../controllers/media-controller.js\";\nimport ButtonStyles from \"../styles/buttons.js\";\nimport RecordingButtonStyles from \"../styles/recording-button.js\";\nimport type { ProxyOptions, RecordingState } from \"../types.js\";\nimport {\n audioLevelChangedEvent,\n commandEvent,\n errorEvent,\n networkActivityEvent,\n type RecordingStateChangedEventDetail,\n recordingStateChangedEvent,\n streamClosedEvent,\n transcriptEvent,\n usageEvent,\n} from \"../utils/events.js\";\n\nimport \"./audio-visualiser.js\";\nimport \"../icons/icons.js\";\n\n@customElement(\"dictation-recording-button\")\nexport class DictationRecordingButton extends LitElement {\n @consume({ context: recordingStateContext, subscribe: true })\n @state()\n _recordingState: RecordingState = \"stopped\";\n\n @consume({ context: selectedDeviceContext, subscribe: true })\n @state()\n _selectedDevice?: MediaDeviceInfo;\n\n @consume({ context: accessTokenContext, subscribe: true })\n @state()\n _accessToken?: string;\n\n @consume({ context: authConfigContext, subscribe: true })\n @state()\n _authConfig?: Corti.BearerOptions;\n\n @consume({ context: regionContext, subscribe: true })\n @state()\n _region?: string;\n\n @consume({ context: tenantNameContext, subscribe: true })\n @state()\n _tenantName?: string;\n\n @consume({ context: dictationConfigContext, subscribe: true })\n @state()\n _dictationConfig?: Corti.TranscribeConfig;\n\n @consume({ context: socketUrlContext, subscribe: true })\n @state()\n _socketUrl?: string;\n\n @consume({ context: socketProxyContext, subscribe: true })\n @state()\n _socketProxy?: ProxyOptions;\n\n @consume({ context: debugDisplayAudioContext, subscribe: true })\n @state()\n _debug_displayAudio?: boolean;\n\n @consume({ context: pushToTalkKeybindingContext, subscribe: true })\n @state()\n _pushToTalkKeybinding?: string | null;\n\n @consume({ context: toggleToTalkKeybindingContext, subscribe: true })\n @state()\n _toggleToTalkKeybinding?: string | null;\n\n @property({ type: Boolean })\n allowButtonFocus: boolean = false;\n\n #mediaController = new MediaController(this);\n #dictationController = new DictationController(this);\n #keybindingController = new KeybindingController(this);\n #closeConnectionOnInit = false;\n #processing = false;\n #connection: RecordingStateChangedEventDetail[\"connection\"] = \"CLOSED\";\n\n static styles: CSSResultGroup = [RecordingButtonStyles, ButtonStyles];\n\n protected update(changedProperties: PropertyValues) {\n if (\n changedProperties.has(\"_recordingState\") &&\n this._recordingState === \"recording\" &&\n this.#closeConnectionOnInit\n ) {\n this.#closeConnectionOnInit = false;\n this.#handleStop();\n }\n\n super.update(changedProperties);\n }\n\n #handleClick(event: MouseEvent): void {\n if (!this.allowButtonFocus) {\n event.preventDefault();\n }\n\n this.toggleRecording();\n }\n\n #handleWebSocketMessage = (message: TranscribeMessage): void => {\n switch (message.type) {\n case \"CONFIG_ACCEPTED\":\n this.#mediaController.addDataHandler(\n this.#dictationController.mediaRecorderHandler,\n );\n\n if (this._recordingState === \"initializing\") {\n this.#processing = true;\n this.#dispatchRecordingStateChanged(\"recording\");\n }\n break;\n case \"CONFIG_DENIED\":\n this.dispatchEvent(\n errorEvent(`Config denied: ${message.reason ?? \"Unknown reason\"}`),\n );\n this.#handleStop();\n break;\n case \"CONFIG_TIMEOUT\":\n this.dispatchEvent(errorEvent(\"Config timeout\"));\n this.#handleStop();\n break;\n case \"transcript\":\n this.dispatchEvent(transcriptEvent(message));\n break;\n case \"command\":\n this.dispatchEvent(commandEvent(message));\n break;\n case \"usage\":\n this.dispatchEvent(usageEvent(message));\n break;\n case \"error\":\n this.dispatchEvent(errorEvent(message.error));\n this.#handleStop();\n break;\n case \"flushed\":\n if (\n this._recordingState === \"stopped\" ||\n this._recordingState === \"stopping\"\n ) {\n this.#processing = false;\n this.#dispatchRecordingStateChanged(this._recordingState);\n }\n break;\n }\n };\n\n #handleWebSocketError = (error: Error): void => {\n this.dispatchEvent(errorEvent(\"Socket error: \" + error.message));\n this.#processing = false;\n this.#connection = \"CLOSED\";\n this.#handleStop();\n };\n\n #handleWebSocketClose = (event: unknown): void => {\n // When we already have new socket opened\n if (this.#dictationController.isConnectionOpen()) {\n return;\n }\n\n this.#processing = false;\n this.#connection = \"CLOSED\";\n this.dispatchEvent(streamClosedEvent(event));\n this.#dispatchRecordingStateChanged(this._recordingState);\n };\n\n #dispatchRecordingStateChanged(state: RecordingState): void {\n this.dispatchEvent(\n recordingStateChangedEvent(state, {\n connection: this.#connection,\n processing: this.#processing,\n }),\n );\n }\n\n async #handleStart(): Promise<void> {\n this.#dispatchRecordingStateChanged(\"initializing\");\n\n try {\n await this.#mediaController.initialize(() => {\n if (this._recordingState === \"recording\") {\n this.dispatchEvent(errorEvent(\"Recording device access was lost.\"));\n this.#handleStop();\n }\n });\n this.#mediaController.mediaRecorder?.start(AUDIO_CHUNK_INTERVAL_MS);\n this.#mediaController.startAudioLevelMonitoring((level) => {\n this.dispatchEvent(audioLevelChangedEvent(level));\n });\n\n if (this.#connection !== \"OPEN\") {\n this.#connection = \"CONNECTING\";\n }\n\n this.#dispatchRecordingStateChanged(\"recording\");\n\n const isNewConnection = await this.#dictationController.connect(\n this._dictationConfig,\n {\n onClose: this.#handleWebSocketClose,\n onError: this.#handleWebSocketError,\n onMessage: this.#handleWebSocketMessage,\n onNetworkActivity: (direction, data) => {\n this.dispatchEvent(networkActivityEvent(direction, data));\n },\n },\n );\n\n if (!isNewConnection) {\n this.#mediaController.addDataHandler(\n this.#dictationController.mediaRecorderHandler,\n );\n this.#processing = true;\n }\n\n this.#connection = \"OPEN\";\n\n this.#dispatchRecordingStateChanged(\"recording\");\n } catch (error) {\n this.dispatchEvent(errorEvent(error));\n await this.#handleStop();\n }\n }\n\n async #handleStop(): Promise<void> {\n this.#dispatchRecordingStateChanged(\"stopping\");\n\n try {\n this.#mediaController.stopAudioLevelMonitoring();\n await this.#mediaController.stopRecording();\n this.#mediaController.removeDataHandler();\n\n this.#dispatchRecordingStateChanged(\"stopped\");\n\n await this.#dictationController.pause();\n await this.#mediaController.cleanup();\n } catch (error) {\n this.dispatchEvent(errorEvent(error));\n }\n }\n\n public startRecording(): void {\n if (this._recordingState !== \"stopped\") {\n return;\n }\n\n this.#handleStart();\n }\n\n public stopRecording(): void {\n if (\n this._recordingState === \"stopped\" ||\n this._recordingState === \"stopping\"\n ) {\n return;\n }\n\n if (this._recordingState === \"initializing\") {\n this.#closeConnectionOnInit = true;\n return;\n }\n\n this.#handleStop();\n }\n\n public toggleRecording(): void {\n if (this._recordingState === \"stopped\") {\n this.startRecording();\n } else if (this._recordingState === \"recording\") {\n this.stopRecording();\n }\n }\n\n public async openConnection(): Promise<void> {\n if (this._recordingState !== \"stopped\" || this.#processing) {\n return;\n }\n\n if (this.#dictationController.isConnectionOpen()) {\n return;\n }\n\n try {\n this.#connection = \"CONNECTING\";\n this.#dispatchRecordingStateChanged(\"stopped\");\n\n await this.#dictationController.connect(this._dictationConfig, {\n onClose: this.#handleWebSocketClose,\n onError: this.#handleWebSocketError,\n onMessage: this.#handleWebSocketMessage,\n onNetworkActivity: (direction, data) => {\n this.dispatchEvent(networkActivityEvent(direction, data));\n },\n });\n\n this.#connection = \"OPEN\";\n this.#dispatchRecordingStateChanged(\"stopped\");\n } catch (error) {\n this.#connection = \"CLOSED\";\n this.dispatchEvent(errorEvent(error));\n }\n }\n\n public async closeConnection(): Promise<void> {\n if (this._recordingState !== \"stopped\" || this.#processing) {\n return;\n }\n\n if (!this.#dictationController.isConnectionOpen()) {\n this.#connection = \"CLOSED\";\n this.#dispatchRecordingStateChanged(\"stopped\");\n return;\n }\n\n try {\n this.#connection = \"CLOSING\";\n this.#dispatchRecordingStateChanged(\"stopped\");\n await this.#dictationController.closeConnection(\n this.#handleWebSocketClose,\n );\n } catch (error) {\n this.dispatchEvent(errorEvent(error));\n }\n }\n\n render() {\n const isLoading =\n this._recordingState === \"initializing\" ||\n this._recordingState === \"stopping\";\n const isRecording = this._recordingState === \"recording\";\n\n return html`\n <button\n @click=${this.#handleClick}\n ?disabled=${isLoading}\n class=${isRecording ? \"red\" : \"accent\"}\n aria-label=${isRecording ? \"Stop recording\" : \"Start recording\"}\n aria-pressed=${isRecording}\n >\n ${\n isLoading\n ? html`<icon-loading-spinner />`\n : isRecording\n ? html`<icon-recording />`\n : html`<icon-mic-on />`\n }\n <dictation-audio-visualiser\n .level=${this.#mediaController.audioLevel}\n ?active=${isRecording}\n />\n </button>\n `;\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n \"dictation-recording-button\": DictationRecordingButton;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"recording-button.js","sourceRoot":"","sources":["../../src/components/recording-button.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AACA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAEL,IAAI,EACJ,UAAU,GAEX,MAAM,KAAK,CAAC;AACb,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,wBAAwB,EACxB,sBAAsB,EACtB,2BAA2B,EAC3B,qBAAqB,EACrB,aAAa,EACb,qBAAqB,EACrB,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,6BAA6B,GAC9B,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EACL,mBAAmB,GAEpB,MAAM,wCAAwC,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,yCAAyC,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,YAAY,MAAM,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;IAClE,KAAK,EAAE;uEAC8B;AAItC;IAFC,OAAO,CAAC,EAAE,OAAO,EAAE,6BAA6B,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IACpE,KAAK,EAAE;yEACgC;AAGxC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;kEACM;AAlDvB,wBAAwB;IADpC,aAAa,CAAC,4BAA4B,CAAC;GAC/B,wBAAwB,CA0UpC","sourcesContent":["import type { Corti } from \"@corti/sdk\";\nimport { consume } from \"@lit/context\";\nimport {\n type CSSResultGroup,\n html,\n LitElement,\n type PropertyValues,\n} from \"lit\";\nimport { customElement, property, state } from \"lit/decorators.js\";\nimport { AUDIO_CHUNK_INTERVAL_MS } from \"../constants.js\";\nimport {\n accessTokenContext,\n authConfigContext,\n debugDisplayAudioContext,\n dictationConfigContext,\n pushToTalkKeybindingContext,\n recordingStateContext,\n regionContext,\n selectedDeviceContext,\n socketProxyContext,\n socketUrlContext,\n tenantNameContext,\n toggleToTalkKeybindingContext,\n} from \"../contexts/dictation-context.js\";\nimport {\n DictationController,\n type TranscribeMessage,\n} from \"../controllers/dictation-controller.js\";\nimport { KeybindingController } from \"../controllers/keybinding-controller.js\";\nimport { MediaController } from \"../controllers/media-controller.js\";\nimport ButtonStyles from \"../styles/buttons.js\";\nimport RecordingButtonStyles from \"../styles/recording-button.js\";\nimport type { ProxyOptions, RecordingState } from \"../types.js\";\nimport {\n audioLevelChangedEvent,\n commandEvent,\n errorEvent,\n networkActivityEvent,\n type RecordingStateChangedEventDetail,\n recordingStateChangedEvent,\n streamClosedEvent,\n transcriptEvent,\n usageEvent,\n} from \"../utils/events.js\";\n\nimport \"./audio-visualiser.js\";\nimport \"../icons/icons.js\";\n\n@customElement(\"dictation-recording-button\")\nexport class DictationRecordingButton extends LitElement {\n @consume({ context: recordingStateContext, subscribe: true })\n @state()\n _recordingState: RecordingState = \"stopped\";\n\n @consume({ context: selectedDeviceContext, subscribe: true })\n @state()\n _selectedDevice?: MediaDeviceInfo;\n\n @consume({ context: accessTokenContext, subscribe: true })\n @state()\n _accessToken?: string;\n\n @consume({ context: authConfigContext, subscribe: true })\n @state()\n _authConfig?: Corti.BearerOptions;\n\n @consume({ context: regionContext, subscribe: true })\n @state()\n _region?: string;\n\n @consume({ context: tenantNameContext, subscribe: true })\n @state()\n _tenantName?: string;\n\n @consume({ context: dictationConfigContext, subscribe: true })\n @state()\n _dictationConfig?: Corti.TranscribeConfig;\n\n @consume({ context: socketUrlContext, subscribe: true })\n @state()\n _socketUrl?: string;\n\n @consume({ context: socketProxyContext, subscribe: true })\n @state()\n _socketProxy?: ProxyOptions;\n\n @consume({ context: debugDisplayAudioContext, subscribe: true })\n @state()\n _debug_displayAudio?: boolean;\n\n @consume({ context: pushToTalkKeybindingContext, subscribe: true })\n @state()\n _pushToTalkKeybinding?: string | null;\n\n @consume({ context: toggleToTalkKeybindingContext, subscribe: true })\n @state()\n _toggleToTalkKeybinding?: string | null;\n\n @property({ type: Boolean })\n allowButtonFocus: boolean = false;\n\n #mediaController = new MediaController(this);\n #dictationController = new DictationController(this);\n #keybindingController = new KeybindingController(this);\n #closeConnectionOnInit = false;\n #processing = false;\n #connection: RecordingStateChangedEventDetail[\"connection\"] = \"CLOSED\";\n\n static styles: CSSResultGroup = [RecordingButtonStyles, ButtonStyles];\n\n protected update(changedProperties: PropertyValues) {\n if (\n changedProperties.has(\"_recordingState\") &&\n this._recordingState === \"recording\" &&\n this.#closeConnectionOnInit\n ) {\n this.#closeConnectionOnInit = false;\n this.#handleStop();\n }\n\n super.update(changedProperties);\n }\n\n #handleClick(event: MouseEvent): void {\n if (!this.allowButtonFocus) {\n event.preventDefault();\n }\n\n this.toggleRecording();\n }\n\n #handleWebSocketMessage = (message: TranscribeMessage): void => {\n switch (message.type) {\n case \"CONFIG_DENIED\":\n this.dispatchEvent(\n errorEvent(`Config denied: ${message.reason ?? \"Unknown reason\"}`),\n );\n this.#handleStop();\n break;\n case \"CONFIG_TIMEOUT\":\n this.dispatchEvent(errorEvent(\"Config timeout\"));\n this.#handleStop();\n break;\n case \"transcript\":\n this.dispatchEvent(transcriptEvent(message));\n break;\n case \"command\":\n this.dispatchEvent(commandEvent(message));\n break;\n case \"usage\":\n this.dispatchEvent(usageEvent(message));\n break;\n case \"error\":\n this.dispatchEvent(errorEvent(message.error));\n this.#handleStop();\n break;\n case \"flushed\":\n if (\n this._recordingState === \"stopped\" ||\n this._recordingState === \"stopping\"\n ) {\n this.#processing = false;\n this.#dispatchRecordingStateChanged(this._recordingState);\n }\n break;\n }\n };\n\n #handleWebSocketError = (error: Error): void => {\n this.dispatchEvent(errorEvent(\"Socket error: \" + error.message));\n this.#processing = false;\n this.#connection = \"CLOSED\";\n this.#handleStop();\n };\n\n #handleWebSocketClose = (event: unknown): void => {\n // When we already have new socket opened\n if (\n this.#dictationController.isConnectionOpen() ||\n this.#dictationController.isConnecting()\n ) {\n return;\n }\n\n this.#processing = false;\n this.#connection = \"CLOSED\";\n this.dispatchEvent(streamClosedEvent(event));\n this.#dispatchRecordingStateChanged(this._recordingState);\n };\n\n #dispatchRecordingStateChanged(state: RecordingState): void {\n this.dispatchEvent(\n recordingStateChangedEvent(state, {\n connection: this.#connection,\n processing: this.#processing,\n }),\n );\n }\n\n async #handleStart(): Promise<void> {\n this.#dispatchRecordingStateChanged(\"initializing\");\n\n try {\n await this.#mediaController.initialize(() => {\n if (this._recordingState === \"recording\") {\n this.dispatchEvent(errorEvent(\"Recording device access was lost.\"));\n this.#handleStop();\n }\n }, this.#dictationController.mediaRecorderHandler);\n this.#mediaController.mediaRecorder?.start(AUDIO_CHUNK_INTERVAL_MS);\n this.#mediaController.startAudioLevelMonitoring((level) => {\n this.dispatchEvent(audioLevelChangedEvent(level));\n });\n\n this.#processing = true;\n\n if (this.#connection !== \"OPEN\") {\n this.#connection = \"CONNECTING\";\n }\n\n this.#dispatchRecordingStateChanged(\"recording\");\n\n const isNewConnection = await this.#dictationController.connect(\n this._dictationConfig,\n {\n onClose: this.#handleWebSocketClose,\n onError: this.#handleWebSocketError,\n onMessage: this.#handleWebSocketMessage,\n onNetworkActivity: (direction, data) => {\n this.dispatchEvent(networkActivityEvent(direction, data));\n },\n },\n );\n\n if (isNewConnection === \"superseded\") {\n return;\n }\n\n this.#connection = \"OPEN\";\n\n this.#dispatchRecordingStateChanged(this._recordingState);\n } catch (error) {\n this.dispatchEvent(errorEvent(error));\n await this.#handleStop();\n }\n }\n\n async #handleStop(): Promise<void> {\n this.#dispatchRecordingStateChanged(\"stopping\");\n\n try {\n this.#mediaController.stopAudioLevelMonitoring();\n await this.#mediaController.stopRecording();\n\n this.#dispatchRecordingStateChanged(\"stopped\");\n\n await this.#dictationController.pause();\n await this.#mediaController.cleanup();\n } catch (error) {\n this.dispatchEvent(errorEvent(error));\n }\n }\n\n public startRecording(): void {\n if (this._recordingState !== \"stopped\") {\n return;\n }\n\n this.#handleStart();\n }\n\n public stopRecording(): void {\n if (\n this._recordingState === \"stopped\" ||\n this._recordingState === \"stopping\"\n ) {\n return;\n }\n\n if (this._recordingState === \"initializing\") {\n this.#closeConnectionOnInit = true;\n return;\n }\n\n this.#handleStop();\n }\n\n public toggleRecording(): void {\n if (this._recordingState === \"stopped\") {\n this.startRecording();\n } else if (this._recordingState === \"recording\") {\n this.stopRecording();\n }\n }\n\n public async openConnection(): Promise<void> {\n if (this._recordingState !== \"stopped\" || this.#processing) {\n return;\n }\n\n if (this.#dictationController.isConnectionOpen()) {\n return;\n }\n\n try {\n this.#connection = \"CONNECTING\";\n this.#dispatchRecordingStateChanged(this._recordingState);\n\n await this.#dictationController.connect(this._dictationConfig, {\n onClose: this.#handleWebSocketClose,\n onError: this.#handleWebSocketError,\n onMessage: this.#handleWebSocketMessage,\n onNetworkActivity: (direction, data) => {\n this.dispatchEvent(networkActivityEvent(direction, data));\n },\n });\n\n this.#connection = \"OPEN\";\n this.#dispatchRecordingStateChanged(this._recordingState);\n } catch (error) {\n this.#connection = \"CLOSED\";\n this.dispatchEvent(errorEvent(error));\n }\n }\n\n public async closeConnection(): Promise<void> {\n if (this._recordingState !== \"stopped\" || this.#processing) {\n return;\n }\n\n if (this.#dictationController.isConnecting()) {\n await this.#dictationController.waitForConnection();\n }\n\n if (!this.#dictationController.isConnectionOpen()) {\n this.#connection = \"CLOSED\";\n this.#dispatchRecordingStateChanged(\"stopped\");\n return;\n }\n\n try {\n this.#connection = \"CLOSING\";\n this.#dispatchRecordingStateChanged(\"stopped\");\n await this.#dictationController.closeConnection(\n this.#handleWebSocketClose,\n );\n } catch (error) {\n this.dispatchEvent(errorEvent(error));\n }\n }\n\n render() {\n const isLoading =\n this._recordingState === \"initializing\" ||\n this._recordingState === \"stopping\";\n const isRecording = this._recordingState === \"recording\";\n\n return html`\n <button\n @click=${this.#handleClick}\n ?disabled=${isLoading}\n class=${isRecording ? \"red\" : \"accent\"}\n aria-label=${isRecording ? \"Stop recording\" : \"Start recording\"}\n aria-pressed=${isRecording}\n >\n ${\n isLoading\n ? html`<icon-loading-spinner />`\n : isRecording\n ? html`<icon-recording />`\n : html`<icon-mic-on />`\n }\n <dictation-audio-visualiser\n .level=${this.#mediaController.audioLevel}\n ?active=${isRecording}\n />\n </button>\n `;\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n \"dictation-recording-button\": DictationRecordingButton;\n }\n}\n"]}
|
|
@@ -2,6 +2,7 @@ import { type Corti } from "@corti/sdk";
|
|
|
2
2
|
import type { ReactiveController, ReactiveControllerHost } from "lit";
|
|
3
3
|
import type { ProxyOptions } from "../types.js";
|
|
4
4
|
interface DictationControllerHost extends ReactiveControllerHost {
|
|
5
|
+
dispatchEvent: (event: Event) => void;
|
|
5
6
|
_accessToken?: string;
|
|
6
7
|
_authConfig?: Corti.BearerOptions;
|
|
7
8
|
_region?: string;
|
|
@@ -22,10 +23,12 @@ export declare class DictationController implements ReactiveController {
|
|
|
22
23
|
host: DictationControllerHost;
|
|
23
24
|
constructor(host: DictationControllerHost);
|
|
24
25
|
hostDisconnected(): void;
|
|
25
|
-
connect(dictationConfig?: Corti.TranscribeConfig, callbacks?: WebSocketCallbacks): Promise<boolean>;
|
|
26
|
+
connect(dictationConfig?: Corti.TranscribeConfig, callbacks?: WebSocketCallbacks): Promise<boolean | "superseded">;
|
|
26
27
|
mediaRecorderHandler: (data: Blob) => void;
|
|
27
28
|
pause(): Promise<void>;
|
|
28
29
|
isConnectionOpen(): boolean;
|
|
30
|
+
isConnecting(): boolean;
|
|
31
|
+
waitForConnection(): Promise<void>;
|
|
29
32
|
closeConnection(onClose?: (event: unknown) => void): Promise<void>;
|
|
30
33
|
cleanup(): void;
|
|
31
34
|
}
|
|
@@ -9,9 +9,10 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
11
|
};
|
|
12
|
-
var _DictationController_instances, _DictationController_cortiClient, _DictationController_webSocket, _DictationController_closeTimeout, _DictationController_callbacks, _DictationController_lastDictationConfig, _DictationController_lastSocketUrl, _DictationController_lastSocketProxy, _DictationController_configHasChanged, _DictationController_connectProxy, _DictationController_connectAuth, _DictationController_setupWebSocketHandlers;
|
|
12
|
+
var _DictationController_instances, _DictationController_cortiClient, _DictationController_webSocket, _DictationController_closeTimeout, _DictationController_callbacks, _DictationController_lastDictationConfig, _DictationController_lastSocketUrl, _DictationController_lastSocketProxy, _DictationController_outboundQueue, _DictationController_socketReady, _DictationController_connectingPromise, _DictationController_connectionGeneration, _DictationController_isConnecting, _DictationController_configHasChanged, _DictationController_doConnect, _DictationController_connectProxy, _DictationController_connectAuth, _DictationController_setupWebSocketHandlers, _DictationController_drain;
|
|
13
13
|
import { CortiClient, CortiWebSocketProxyClient } from "@corti/sdk";
|
|
14
14
|
import { DEFAULT_DICTATION_CONFIG } from "../constants.js";
|
|
15
|
+
import { errorEvent } from "../utils/events.js";
|
|
15
16
|
export class DictationController {
|
|
16
17
|
constructor(host) {
|
|
17
18
|
_DictationController_instances.add(this);
|
|
@@ -22,12 +23,21 @@ export class DictationController {
|
|
|
22
23
|
_DictationController_lastDictationConfig.set(this, null);
|
|
23
24
|
_DictationController_lastSocketUrl.set(this, void 0);
|
|
24
25
|
_DictationController_lastSocketProxy.set(this, void 0);
|
|
26
|
+
_DictationController_outboundQueue.set(this, []);
|
|
27
|
+
_DictationController_socketReady.set(this, false);
|
|
28
|
+
_DictationController_connectingPromise.set(this, null);
|
|
29
|
+
_DictationController_connectionGeneration.set(this, 0);
|
|
30
|
+
_DictationController_isConnecting.set(this, false);
|
|
25
31
|
this.mediaRecorderHandler = (data) => {
|
|
26
|
-
__classPrivateFieldGet(this,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
if (__classPrivateFieldGet(this, _DictationController_socketReady, "f") && this.isConnectionOpen()) {
|
|
33
|
+
__classPrivateFieldGet(this, _DictationController_webSocket, "f")?.send(data);
|
|
34
|
+
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("sent", {
|
|
35
|
+
size: data.size,
|
|
36
|
+
type: "audio",
|
|
37
|
+
});
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
__classPrivateFieldGet(this, _DictationController_outboundQueue, "f").push(data);
|
|
31
41
|
};
|
|
32
42
|
this.host = host;
|
|
33
43
|
host.addController(this);
|
|
@@ -36,41 +46,49 @@ export class DictationController {
|
|
|
36
46
|
this.cleanup();
|
|
37
47
|
}
|
|
38
48
|
async connect(dictationConfig = DEFAULT_DICTATION_CONFIG, callbacks = {}) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
throw new Error("Already connected. Disconnect before reconnecting.");
|
|
44
|
-
}
|
|
45
|
-
__classPrivateFieldSet(this, _DictationController_webSocket, this.host._socketUrl || this.host._socketProxy
|
|
46
|
-
? await __classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_connectProxy).call(this, dictationConfig)
|
|
47
|
-
: await __classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_connectAuth).call(this, dictationConfig), "f");
|
|
48
|
-
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("sent", {
|
|
49
|
-
configuration: dictationConfig,
|
|
50
|
-
type: "config",
|
|
51
|
-
});
|
|
52
|
-
__classPrivateFieldSet(this, _DictationController_lastDictationConfig, this.host._dictationConfig || null, "f");
|
|
53
|
-
__classPrivateFieldSet(this, _DictationController_lastSocketUrl, this.host._socketUrl, "f");
|
|
54
|
-
__classPrivateFieldSet(this, _DictationController_lastSocketProxy, this.host._socketProxy, "f");
|
|
49
|
+
// If a connection attempt is already in progress with the same config, reuse it
|
|
50
|
+
// to avoid opening multiple sockets when connect() is called concurrently.
|
|
51
|
+
if (__classPrivateFieldGet(this, _DictationController_connectingPromise, "f") && !__classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_configHasChanged).call(this)) {
|
|
52
|
+
return __classPrivateFieldGet(this, _DictationController_connectingPromise, "f");
|
|
55
53
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
// #isConnecting must be set synchronously before #doConnect runs, because
|
|
55
|
+
// #doConnect calls cleanup() which closes the old socket, firing its "close"
|
|
56
|
+
// event synchronously. Handlers that check isConnecting() need to see true
|
|
57
|
+
// at that point — before #connectingPromise is even assigned.
|
|
58
|
+
__classPrivateFieldSet(this, _DictationController_isConnecting, true, "f");
|
|
59
|
+
__classPrivateFieldSet(this, _DictationController_connectingPromise, __classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_doConnect).call(this, dictationConfig, callbacks).finally(() => {
|
|
60
|
+
__classPrivateFieldSet(this, _DictationController_isConnecting, false, "f");
|
|
61
|
+
__classPrivateFieldSet(this, _DictationController_connectingPromise, null, "f");
|
|
62
|
+
}), "f");
|
|
63
|
+
return __classPrivateFieldGet(this, _DictationController_connectingPromise, "f");
|
|
59
64
|
}
|
|
60
65
|
async pause() {
|
|
61
|
-
__classPrivateFieldGet(this,
|
|
62
|
-
|
|
66
|
+
if (__classPrivateFieldGet(this, _DictationController_socketReady, "f") && this.isConnectionOpen()) {
|
|
67
|
+
__classPrivateFieldGet(this, _DictationController_webSocket, "f")?.send(JSON.stringify({ type: "flush" }));
|
|
68
|
+
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("sent", { type: "flush" });
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
__classPrivateFieldGet(this, _DictationController_outboundQueue, "f").push({ type: "flush" });
|
|
63
72
|
}
|
|
64
73
|
isConnectionOpen() {
|
|
65
74
|
return (__classPrivateFieldGet(this, _DictationController_webSocket, "f") !== null &&
|
|
66
75
|
(__classPrivateFieldGet(this, _DictationController_webSocket, "f").readyState === WebSocket.OPEN ||
|
|
67
76
|
__classPrivateFieldGet(this, _DictationController_webSocket, "f").readyState === WebSocket.CONNECTING));
|
|
68
77
|
}
|
|
78
|
+
isConnecting() {
|
|
79
|
+
return __classPrivateFieldGet(this, _DictationController_isConnecting, "f");
|
|
80
|
+
}
|
|
81
|
+
async waitForConnection() {
|
|
82
|
+
await __classPrivateFieldGet(this, _DictationController_connectingPromise, "f");
|
|
83
|
+
}
|
|
69
84
|
async closeConnection(onClose) {
|
|
70
85
|
await new Promise((resolve, reject) => {
|
|
71
86
|
const oldSocket = __classPrivateFieldGet(this, _DictationController_webSocket, "f");
|
|
72
87
|
__classPrivateFieldSet(this, _DictationController_webSocket, null, "f");
|
|
73
|
-
if (!oldSocket ||
|
|
88
|
+
if (!oldSocket ||
|
|
89
|
+
(oldSocket.readyState !== WebSocket.OPEN &&
|
|
90
|
+
oldSocket.readyState !== WebSocket.CONNECTING)) {
|
|
91
|
+
__classPrivateFieldSet(this, _DictationController_socketReady, false, "f");
|
|
74
92
|
resolve();
|
|
75
93
|
return;
|
|
76
94
|
}
|
|
@@ -84,11 +102,22 @@ export class DictationController {
|
|
|
84
102
|
}
|
|
85
103
|
resolve();
|
|
86
104
|
});
|
|
105
|
+
const wasReady = __classPrivateFieldGet(this, _DictationController_socketReady, "f");
|
|
106
|
+
__classPrivateFieldSet(this, _DictationController_socketReady, false, "f");
|
|
87
107
|
oldSocket.on("message", (message) => {
|
|
88
108
|
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("received", message);
|
|
89
109
|
if (__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onMessage) {
|
|
90
110
|
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onMessage(message);
|
|
91
111
|
}
|
|
112
|
+
// closeConnection() may be called before CONFIG_ACCEPTED arrives (e.g.
|
|
113
|
+
// openConnection() followed immediately by closeConnection()). We can't
|
|
114
|
+
// use the outbound queue here because #webSocket is already null, so we
|
|
115
|
+
// send "end" directly on oldSocket as soon as config is accepted.
|
|
116
|
+
if (!wasReady && message.type === "CONFIG_ACCEPTED") {
|
|
117
|
+
oldSocket.sendEnd({ type: "end" });
|
|
118
|
+
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("sent", { type: "end" });
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
92
121
|
if (message.type === "ended") {
|
|
93
122
|
if (__classPrivateFieldGet(this, _DictationController_closeTimeout, "f")) {
|
|
94
123
|
clearTimeout(__classPrivateFieldGet(this, _DictationController_closeTimeout, "f"));
|
|
@@ -98,8 +127,10 @@ export class DictationController {
|
|
|
98
127
|
return;
|
|
99
128
|
}
|
|
100
129
|
});
|
|
101
|
-
|
|
102
|
-
|
|
130
|
+
if (wasReady) {
|
|
131
|
+
oldSocket.sendEnd({ type: "end" });
|
|
132
|
+
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("sent", { type: "end" });
|
|
133
|
+
}
|
|
103
134
|
__classPrivateFieldSet(this, _DictationController_closeTimeout, window.setTimeout(() => {
|
|
104
135
|
reject(new Error("Connection close timeout"));
|
|
105
136
|
if (oldSocket?.readyState === WebSocket.OPEN) {
|
|
@@ -109,26 +140,65 @@ export class DictationController {
|
|
|
109
140
|
});
|
|
110
141
|
}
|
|
111
142
|
cleanup() {
|
|
143
|
+
var _a;
|
|
144
|
+
// Incrementing generation invalidates any in-flight #doConnect awaits,
|
|
145
|
+
// causing them to discard their socket and return "superseded".
|
|
146
|
+
__classPrivateFieldSet(this, _DictationController_connectionGeneration, (_a = __classPrivateFieldGet(this, _DictationController_connectionGeneration, "f"), _a++, _a), "f");
|
|
147
|
+
__classPrivateFieldSet(this, _DictationController_socketReady, false, "f");
|
|
112
148
|
if (__classPrivateFieldGet(this, _DictationController_closeTimeout, "f")) {
|
|
113
149
|
clearTimeout(__classPrivateFieldGet(this, _DictationController_closeTimeout, "f"));
|
|
114
150
|
__classPrivateFieldSet(this, _DictationController_closeTimeout, undefined, "f");
|
|
115
151
|
}
|
|
116
|
-
if (
|
|
117
|
-
__classPrivateFieldGet(this, _DictationController_webSocket, "f")
|
|
152
|
+
if (this.isConnectionOpen()) {
|
|
153
|
+
__classPrivateFieldGet(this, _DictationController_webSocket, "f")?.close();
|
|
118
154
|
}
|
|
119
155
|
__classPrivateFieldSet(this, _DictationController_webSocket, null, "f");
|
|
120
156
|
__classPrivateFieldSet(this, _DictationController_cortiClient, null, "f");
|
|
121
157
|
__classPrivateFieldSet(this, _DictationController_lastDictationConfig, null, "f");
|
|
122
158
|
__classPrivateFieldSet(this, _DictationController_lastSocketUrl, undefined, "f");
|
|
123
159
|
__classPrivateFieldSet(this, _DictationController_lastSocketProxy, undefined, "f");
|
|
160
|
+
if (__classPrivateFieldGet(this, _DictationController_outboundQueue, "f").length > 0) {
|
|
161
|
+
this.host.dispatchEvent(errorEvent(`${__classPrivateFieldGet(this, _DictationController_outboundQueue, "f").length} unsent audio message(s) were discarded because the configuration changed before the connection was closed`));
|
|
162
|
+
}
|
|
163
|
+
__classPrivateFieldSet(this, _DictationController_outboundQueue, [], "f");
|
|
124
164
|
}
|
|
125
165
|
}
|
|
126
|
-
_DictationController_cortiClient = new WeakMap(), _DictationController_webSocket = new WeakMap(), _DictationController_closeTimeout = new WeakMap(), _DictationController_callbacks = new WeakMap(), _DictationController_lastDictationConfig = new WeakMap(), _DictationController_lastSocketUrl = new WeakMap(), _DictationController_lastSocketProxy = new WeakMap(), _DictationController_instances = new WeakSet(), _DictationController_configHasChanged = function _DictationController_configHasChanged() {
|
|
166
|
+
_DictationController_cortiClient = new WeakMap(), _DictationController_webSocket = new WeakMap(), _DictationController_closeTimeout = new WeakMap(), _DictationController_callbacks = new WeakMap(), _DictationController_lastDictationConfig = new WeakMap(), _DictationController_lastSocketUrl = new WeakMap(), _DictationController_lastSocketProxy = new WeakMap(), _DictationController_outboundQueue = new WeakMap(), _DictationController_socketReady = new WeakMap(), _DictationController_connectingPromise = new WeakMap(), _DictationController_connectionGeneration = new WeakMap(), _DictationController_isConnecting = new WeakMap(), _DictationController_instances = new WeakSet(), _DictationController_configHasChanged = function _DictationController_configHasChanged() {
|
|
127
167
|
return (JSON.stringify(this.host._dictationConfig) !==
|
|
128
168
|
JSON.stringify(__classPrivateFieldGet(this, _DictationController_lastDictationConfig, "f")) ||
|
|
129
169
|
this.host._socketUrl !== __classPrivateFieldGet(this, _DictationController_lastSocketUrl, "f") ||
|
|
130
170
|
JSON.stringify(this.host._socketProxy) !==
|
|
131
171
|
JSON.stringify(__classPrivateFieldGet(this, _DictationController_lastSocketProxy, "f")));
|
|
172
|
+
}, _DictationController_doConnect = async function _DictationController_doConnect(dictationConfig, callbacks) {
|
|
173
|
+
const newConnection = __classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_configHasChanged).call(this) || !this.isConnectionOpen();
|
|
174
|
+
if (newConnection) {
|
|
175
|
+
this.cleanup();
|
|
176
|
+
__classPrivateFieldSet(this, _DictationController_lastDictationConfig, this.host._dictationConfig || null, "f");
|
|
177
|
+
__classPrivateFieldSet(this, _DictationController_lastSocketUrl, this.host._socketUrl, "f");
|
|
178
|
+
__classPrivateFieldSet(this, _DictationController_lastSocketProxy, this.host._socketProxy, "f");
|
|
179
|
+
const generation = __classPrivateFieldGet(this, _DictationController_connectionGeneration, "f");
|
|
180
|
+
const socket = this.host._socketUrl || this.host._socketProxy
|
|
181
|
+
? await __classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_connectProxy).call(this, dictationConfig)
|
|
182
|
+
: await __classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_connectAuth).call(this, dictationConfig);
|
|
183
|
+
// If cleanup() was called while we were awaiting (e.g. config changed),
|
|
184
|
+
// the generation counter will have advanced — discard this stale socket.
|
|
185
|
+
if (__classPrivateFieldGet(this, _DictationController_connectionGeneration, "f") !== generation) {
|
|
186
|
+
socket.close();
|
|
187
|
+
return "superseded";
|
|
188
|
+
}
|
|
189
|
+
__classPrivateFieldSet(this, _DictationController_webSocket, socket, "f");
|
|
190
|
+
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("sent", {
|
|
191
|
+
configuration: dictationConfig,
|
|
192
|
+
type: "config",
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
__classPrivateFieldSet(this, _DictationController_callbacks, callbacks, "f");
|
|
196
|
+
__classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_setupWebSocketHandlers).call(this, callbacks);
|
|
197
|
+
if (!newConnection && this.isConnectionOpen()) {
|
|
198
|
+
__classPrivateFieldSet(this, _DictationController_socketReady, true, "f");
|
|
199
|
+
__classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_drain).call(this);
|
|
200
|
+
}
|
|
201
|
+
return newConnection;
|
|
132
202
|
}, _DictationController_connectProxy = async function _DictationController_connectProxy(dictationConfig) {
|
|
133
203
|
const proxyOptions = this.host._socketProxy || {
|
|
134
204
|
url: this.host._socketUrl || "",
|
|
@@ -164,20 +234,50 @@ _DictationController_cortiClient = new WeakMap(), _DictationController_webSocket
|
|
|
164
234
|
throw new Error("WebSocket not initialized");
|
|
165
235
|
}
|
|
166
236
|
__classPrivateFieldGet(this, _DictationController_webSocket, "f").on("message", (message) => {
|
|
237
|
+
if (message.type === "CONFIG_ACCEPTED") {
|
|
238
|
+
__classPrivateFieldSet(this, _DictationController_socketReady, true, "f");
|
|
239
|
+
__classPrivateFieldGet(this, _DictationController_instances, "m", _DictationController_drain).call(this);
|
|
240
|
+
}
|
|
167
241
|
callbacks.onNetworkActivity?.("received", message);
|
|
168
242
|
if (callbacks.onMessage) {
|
|
169
243
|
callbacks.onMessage(message);
|
|
170
244
|
}
|
|
171
245
|
});
|
|
172
246
|
__classPrivateFieldGet(this, _DictationController_webSocket, "f").on("error", (event) => {
|
|
247
|
+
__classPrivateFieldSet(this, _DictationController_socketReady, false, "f");
|
|
173
248
|
if (callbacks.onError) {
|
|
174
249
|
callbacks.onError(event);
|
|
175
250
|
}
|
|
176
251
|
});
|
|
177
252
|
__classPrivateFieldGet(this, _DictationController_webSocket, "f").on("close", (event) => {
|
|
253
|
+
__classPrivateFieldSet(this, _DictationController_socketReady, false, "f");
|
|
178
254
|
if (callbacks.onClose) {
|
|
179
255
|
callbacks.onClose(event);
|
|
180
256
|
}
|
|
181
257
|
});
|
|
258
|
+
}, _DictationController_drain = function _DictationController_drain() {
|
|
259
|
+
if (!__classPrivateFieldGet(this, _DictationController_socketReady, "f") ||
|
|
260
|
+
!this.isConnectionOpen() ||
|
|
261
|
+
__classPrivateFieldGet(this, _DictationController_outboundQueue, "f").length === 0) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
while (__classPrivateFieldGet(this, _DictationController_outboundQueue, "f").length > 0 && this.isConnectionOpen()) {
|
|
265
|
+
const item = __classPrivateFieldGet(this, _DictationController_outboundQueue, "f").shift();
|
|
266
|
+
if (item === undefined) {
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
if (item instanceof Blob) {
|
|
270
|
+
__classPrivateFieldGet(this, _DictationController_webSocket, "f").send(item);
|
|
271
|
+
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("sent", {
|
|
272
|
+
size: item.size,
|
|
273
|
+
type: "audio",
|
|
274
|
+
});
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
__classPrivateFieldGet(this, _DictationController_webSocket, "f").send(JSON.stringify(item));
|
|
278
|
+
__classPrivateFieldGet(this, _DictationController_callbacks, "f")?.onNetworkActivity?.("sent", {
|
|
279
|
+
type: item.type,
|
|
280
|
+
});
|
|
281
|
+
}
|
|
182
282
|
};
|
|
183
283
|
//# sourceMappingURL=dictation-controller.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dictation-controller.js","sourceRoot":"","sources":["../../src/controllers/dictation-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAc,WAAW,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEhF,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAiC3D,MAAM,OAAO,mBAAmB;IAW9B,YAAY,IAA6B;;QARzC,2CAAmC,IAAI,EAAC;QACxC,yCAAsC,IAAI,EAAC;QAC3C,oDAAuB;QACvB,iDAAgC;QAChC,mDAAsD,IAAI,EAAC;QAC3D,qDAAwB;QACxB,uDAAgC;QA8HhC,yBAAoB,GAAG,CAAC,IAAU,EAAQ,EAAE;YAC1C,uBAAA,IAAI,sCAAW,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC;YACjC,uBAAA,IAAI,sCAAW,EAAE,iBAAiB,EAAE,CAAC,MAAM,EAAE;gBAC3C,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,IAAI,EAAE,OAAO;aACd,CAAC,CAAC;QACL,CAAC,CAAC;QAjIA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;IAED,gBAAgB;QACd,IAAI,C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AAC,KAAc,EAAE,EAAE;QAC7C,IAAI,SAAS,CAAC,OAAO,EAAE,CAAC;YACtB,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { type Corti, CortiClient, CortiWebSocketProxyClient } from \"@corti/sdk\";\nimport type { ReactiveController, ReactiveControllerHost } from \"lit\";\nimport { DEFAULT_DICTATION_CONFIG } from \"../constants.js\";\nimport type { ProxyOptions } from \"../types.js\";\n\ntype TranscribeSocket = Awaited<\n ReturnType<CortiClient[\"transcribe\"][\"connect\"]>\n>;\n\ninterface DictationControllerHost extends ReactiveControllerHost {\n _accessToken?: string;\n _authConfig?: Corti.BearerOptions;\n _region?: string;\n _tenantName?: string;\n _socketUrl?: string;\n _socketProxy?: ProxyOptions;\n _dictationConfig?: Corti.TranscribeConfig;\n}\n\nexport type TranscribeMessage =\n | Corti.TranscribeConfigStatusMessage\n | Corti.TranscribeUsageMessage\n | Corti.TranscribeEndedMessage\n | Corti.TranscribeErrorMessage\n | Corti.TranscribeTranscriptMessage\n | Corti.TranscribeCommandMessage\n | Corti.TranscribeFlushedMessage;\n\ninterface WebSocketCallbacks {\n onMessage?: (message: TranscribeMessage) => void;\n onError?: (error: Error) => void;\n onClose?: (event: unknown) => void;\n onNetworkActivity?: (direction: \"sent\" | \"received\", data: unknown) => void;\n}\n\nexport class DictationController implements ReactiveController {\n host: DictationControllerHost;\n\n #cortiClient: CortiClient | null = null;\n #webSocket: TranscribeSocket | null = null;\n #closeTimeout?: number;\n #callbacks?: WebSocketCallbacks;\n #lastDictationConfig: Corti.TranscribeConfig | null = null;\n #lastSocketUrl?: string;\n #lastSocketProxy?: ProxyOptions;\n\n constructor(host: DictationControllerHost) {\n this.host = host;\n host.addController(this);\n }\n\n hostDisconnected(): void {\n this.cleanup();\n }\n\n #configHasChanged(): boolean {\n return (\n JSON.stringify(this.host._dictationConfig) !==\n JSON.stringify(this.#lastDictationConfig) ||\n this.host._socketUrl !== this.#lastSocketUrl ||\n JSON.stringify(this.host._socketProxy) !==\n JSON.stringify(this.#lastSocketProxy)\n );\n }\n\n async connect(\n dictationConfig: Corti.TranscribeConfig = DEFAULT_DICTATION_CONFIG,\n callbacks: WebSocketCallbacks = {},\n ): Promise<boolean> {\n const newConnection = this.#configHasChanged() || !this.isConnectionOpen();\n\n if (newConnection) {\n this.cleanup();\n\n if (this.#webSocket?.readyState === WebSocket.OPEN) {\n throw new Error(\"Already connected. Disconnect before reconnecting.\");\n }\n\n this.#webSocket =\n this.host._socketUrl || this.host._socketProxy\n ? await this.#connectProxy(dictationConfig)\n : await this.#connectAuth(dictationConfig);\n\n this.#callbacks?.onNetworkActivity?.(\"sent\", {\n configuration: dictationConfig,\n type: \"config\",\n });\n\n this.#lastDictationConfig = this.host._dictationConfig || null;\n this.#lastSocketUrl = this.host._socketUrl;\n this.#lastSocketProxy = this.host._socketProxy;\n }\n\n this.#callbacks = callbacks;\n this.#setupWebSocketHandlers(callbacks);\n\n return newConnection;\n }\n\n async #connectProxy(\n dictationConfig: Corti.TranscribeConfig,\n ): Promise<TranscribeSocket> {\n const proxyOptions = this.host._socketProxy || {\n url: this.host._socketUrl || \"\",\n };\n\n if (!proxyOptions.url) {\n throw new Error(\"Proxy URL is required when using proxy client\");\n }\n\n return await CortiWebSocketProxyClient.transcribe.connect({\n configuration: dictationConfig,\n proxy: proxyOptions,\n });\n }\n\n async #connectAuth(\n dictationConfig: Corti.TranscribeConfig,\n ): Promise<TranscribeSocket> {\n if (!this.host._authConfig && !this.host._accessToken) {\n throw new Error(\n \"Auth configuration or access token is required to connect\",\n );\n }\n\n // Use authConfig if available, otherwise create one from accessToken\n const auth: Corti.BearerOptions = this.host._authConfig || {\n accessToken: this.host._accessToken || \"\",\n refreshAccessToken: () => ({\n accessToken: this.host._accessToken || \"\",\n }),\n };\n\n this.#cortiClient = new CortiClient({\n auth,\n environment: this.host._region,\n tenantName: this.host._tenantName,\n });\n\n return await this.#cortiClient.transcribe.connect({\n configuration: dictationConfig,\n });\n }\n\n #setupWebSocketHandlers(callbacks: WebSocketCallbacks): void {\n if (!this.#webSocket) {\n throw new Error(\"WebSocket not initialized\");\n }\n\n this.#webSocket.on(\"message\", (message: TranscribeMessage) => {\n callbacks.onNetworkActivity?.(\"received\", message);\n\n if (callbacks.onMessage) {\n callbacks.onMessage(message);\n }\n });\n\n this.#webSocket.on(\"error\", (event: Error) => {\n if (callbacks.onError) {\n callbacks.onError(event);\n }\n });\n\n this.#webSocket.on(\"close\", (event: unknown) => {\n if (callbacks.onClose) {\n callbacks.onClose(event);\n }\n });\n }\n\n mediaRecorderHandler = (data: Blob): void => {\n this.#webSocket?.sendAudio(data);\n this.#callbacks?.onNetworkActivity?.(\"sent\", {\n size: data.size,\n type: \"audio\",\n });\n };\n\n async pause(): Promise<void> {\n this.#webSocket?.sendFlush({ type: \"flush\" });\n this.#callbacks?.onNetworkActivity?.(\"sent\", { type: \"flush\" });\n }\n\n isConnectionOpen(): boolean {\n return (\n this.#webSocket !== null &&\n (this.#webSocket.readyState === WebSocket.OPEN ||\n this.#webSocket.readyState === WebSocket.CONNECTING)\n );\n }\n\n async closeConnection(onClose?: (event: unknown) => void): Promise<void> {\n await new Promise<void>((resolve, reject) => {\n const oldSocket = this.#webSocket;\n this.#webSocket = null;\n\n if (!oldSocket || oldSocket.readyState !== WebSocket.OPEN) {\n resolve();\n return;\n }\n\n oldSocket.on(\"close\", (event) => {\n if (this.#closeTimeout) {\n clearTimeout(this.#closeTimeout);\n this.#closeTimeout = undefined;\n }\n\n if (onClose) {\n onClose(event);\n }\n\n resolve();\n });\n\n oldSocket.on(\"message\", (message) => {\n this.#callbacks?.onNetworkActivity?.(\"received\", message);\n\n if (this.#callbacks?.onMessage) {\n this.#callbacks?.onMessage(message);\n }\n\n if (message.type === \"ended\") {\n if (this.#closeTimeout) {\n clearTimeout(this.#closeTimeout);\n this.#closeTimeout = undefined;\n }\n\n resolve();\n return;\n }\n });\n\n oldSocket.sendEnd({ type: \"end\" });\n this.#callbacks?.onNetworkActivity?.(\"sent\", { type: \"end\" });\n\n this.#closeTimeout = window.setTimeout(() => {\n reject(new Error(\"Connection close timeout\"));\n\n if (oldSocket?.readyState === WebSocket.OPEN) {\n oldSocket.close();\n }\n }, 10000);\n });\n }\n\n cleanup(): void {\n if (this.#closeTimeout) {\n clearTimeout(this.#closeTimeout);\n this.#closeTimeout = undefined;\n }\n\n if (this.#webSocket?.readyState === WebSocket.OPEN) {\n this.#webSocket.close();\n }\n\n this.#webSocket = null;\n this.#cortiClient = null;\n this.#lastDictationConfig = null;\n this.#lastSocketUrl = undefined;\n this.#lastSocketProxy = undefined;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"dictation-controller.js","sourceRoot":"","sources":["../../src/controllers/dictation-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAc,WAAW,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEhF,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAE3D,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAsChD,MAAM,OAAO,mBAAmB;IAgB9B,YAAY,IAA6B;;QAbzC,2CAAmC,IAAI,EAAC;QACxC,yCAAsC,IAAI,EAAC;QAC3C,oDAAuB;QACvB,iDAAgC;QAChC,mDAAsD,IAAI,EAAC;QAC3D,qDAAwB;QACxB,uDAAgC;QAChC,6CAAiC,EAAE,EAAC;QACpC,2CAAe,KAAK,EAAC;QACrB,iDAA6D,IAAI,EAAC;QAClE,oDAAwB,CAAC,EAAC;QAC1B,4CAAgB,KAAK,EAAC;QA2MtB,yBAAoB,GAAG,CAAC,IAAU,EAAQ,EAAE;YAC1C,IAAI,uBAAA,IAAI,wCAAa,IAAI,IAAI,CAAC,gBAAgB,EAAE,EAAE,CAAC;gBACjD,uBAAA,IAAI,sCAAW,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC5B,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I,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;QAC5C,uBAAA,IAAI,sCAAW,EAAE,iBAAiB,EAAE,CAAC,MAAM,EAAE;YAC3C,IAAI,EAAE,IAAI,CAAC,IAAI;SAChB,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import { type Corti, CortiClient, CortiWebSocketProxyClient } from \"@corti/sdk\";\nimport type { ReactiveController, ReactiveControllerHost } from \"lit\";\nimport { DEFAULT_DICTATION_CONFIG } from \"../constants.js\";\nimport type { ProxyOptions } from \"../types.js\";\nimport { errorEvent } from \"../utils/events.js\";\n\ntype TranscribeSocket = Awaited<\n ReturnType<CortiClient[\"transcribe\"][\"connect\"]>\n>;\n\ninterface DictationControllerHost extends ReactiveControllerHost {\n dispatchEvent: (event: Event) => void;\n _accessToken?: string;\n _authConfig?: Corti.BearerOptions;\n _region?: string;\n _tenantName?: string;\n _socketUrl?: string;\n _socketProxy?: ProxyOptions;\n _dictationConfig?: Corti.TranscribeConfig;\n}\n\nexport type TranscribeMessage =\n | Corti.TranscribeConfigStatusMessage\n | Corti.TranscribeUsageMessage\n | Corti.TranscribeEndedMessage\n | Corti.TranscribeErrorMessage\n | Corti.TranscribeTranscriptMessage\n | Corti.TranscribeCommandMessage\n | Corti.TranscribeFlushedMessage;\n\ntype OutboundItem =\n | Blob\n | Corti.TranscribeFlushMessage\n | Corti.TranscribeEndMessage;\n\ninterface WebSocketCallbacks {\n onMessage?: (message: TranscribeMessage) => void;\n onError?: (error: Error) => void;\n onClose?: (event: unknown) => void;\n onNetworkActivity?: (direction: \"sent\" | \"received\", data: unknown) => void;\n}\n\nexport class DictationController implements ReactiveController {\n host: DictationControllerHost;\n\n #cortiClient: CortiClient | null = null;\n #webSocket: TranscribeSocket | null = null;\n #closeTimeout?: number;\n #callbacks?: WebSocketCallbacks;\n #lastDictationConfig: Corti.TranscribeConfig | null = null;\n #lastSocketUrl?: string;\n #lastSocketProxy?: ProxyOptions;\n #outboundQueue: OutboundItem[] = [];\n #socketReady = false;\n #connectingPromise: Promise<boolean | \"superseded\"> | null = null;\n #connectionGeneration = 0;\n #isConnecting = false;\n\n constructor(host: DictationControllerHost) {\n this.host = host;\n host.addController(this);\n }\n\n hostDisconnected(): void {\n this.cleanup();\n }\n\n #configHasChanged(): boolean {\n return (\n JSON.stringify(this.host._dictationConfig) !==\n JSON.stringify(this.#lastDictationConfig) ||\n this.host._socketUrl !== this.#lastSocketUrl ||\n JSON.stringify(this.host._socketProxy) !==\n JSON.stringify(this.#lastSocketProxy)\n );\n }\n\n async connect(\n dictationConfig: Corti.TranscribeConfig = DEFAULT_DICTATION_CONFIG,\n callbacks: WebSocketCallbacks = {},\n ): Promise<boolean | \"superseded\"> {\n // If a connection attempt is already in progress with the same config, reuse it\n // to avoid opening multiple sockets when connect() is called concurrently.\n if (this.#connectingPromise && !this.#configHasChanged()) {\n return this.#connectingPromise;\n }\n\n // #isConnecting must be set synchronously before #doConnect runs, because\n // #doConnect calls cleanup() which closes the old socket, firing its \"close\"\n // event synchronously. Handlers that check isConnecting() need to see true\n // at that point — before #connectingPromise is even assigned.\n this.#isConnecting = true;\n this.#connectingPromise = this.#doConnect(\n dictationConfig,\n callbacks,\n ).finally(() => {\n this.#isConnecting = false;\n this.#connectingPromise = null;\n });\n\n return this.#connectingPromise;\n }\n\n async #doConnect(\n dictationConfig: Corti.TranscribeConfig,\n callbacks: WebSocketCallbacks,\n ): Promise<boolean | \"superseded\"> {\n const newConnection = this.#configHasChanged() || !this.isConnectionOpen();\n\n if (newConnection) {\n this.cleanup();\n\n this.#lastDictationConfig = this.host._dictationConfig || null;\n this.#lastSocketUrl = this.host._socketUrl;\n this.#lastSocketProxy = this.host._socketProxy;\n\n const generation = this.#connectionGeneration;\n\n const socket =\n this.host._socketUrl || this.host._socketProxy\n ? await this.#connectProxy(dictationConfig)\n : await this.#connectAuth(dictationConfig);\n\n // If cleanup() was called while we were awaiting (e.g. config changed),\n // the generation counter will have advanced — discard this stale socket.\n if (this.#connectionGeneration !== generation) {\n socket.close();\n return \"superseded\";\n }\n\n this.#webSocket = socket;\n\n this.#callbacks?.onNetworkActivity?.(\"sent\", {\n configuration: dictationConfig,\n type: \"config\",\n });\n }\n\n this.#callbacks = callbacks;\n this.#setupWebSocketHandlers(callbacks);\n\n if (!newConnection && this.isConnectionOpen()) {\n this.#socketReady = true;\n this.#drain();\n }\n\n return newConnection;\n }\n\n async #connectProxy(\n dictationConfig: Corti.TranscribeConfig,\n ): Promise<TranscribeSocket> {\n const proxyOptions = this.host._socketProxy || {\n url: this.host._socketUrl || \"\",\n };\n\n if (!proxyOptions.url) {\n throw new Error(\"Proxy URL is required when using proxy client\");\n }\n\n return await CortiWebSocketProxyClient.transcribe.connect({\n configuration: dictationConfig,\n proxy: proxyOptions,\n });\n }\n\n async #connectAuth(\n dictationConfig: Corti.TranscribeConfig,\n ): Promise<TranscribeSocket> {\n if (!this.host._authConfig && !this.host._accessToken) {\n throw new Error(\n \"Auth configuration or access token is required to connect\",\n );\n }\n\n // Use authConfig if available, otherwise create one from accessToken\n const auth: Corti.BearerOptions = this.host._authConfig || {\n accessToken: this.host._accessToken || \"\",\n refreshAccessToken: () => ({\n accessToken: this.host._accessToken || \"\",\n }),\n };\n\n this.#cortiClient = new CortiClient({\n auth,\n environment: this.host._region,\n tenantName: this.host._tenantName,\n });\n\n return await this.#cortiClient.transcribe.connect({\n configuration: dictationConfig,\n });\n }\n\n #setupWebSocketHandlers(callbacks: WebSocketCallbacks): void {\n if (!this.#webSocket) {\n throw new Error(\"WebSocket not initialized\");\n }\n\n this.#webSocket.on(\"message\", (message: TranscribeMessage) => {\n if (message.type === \"CONFIG_ACCEPTED\") {\n this.#socketReady = true;\n this.#drain();\n }\n\n callbacks.onNetworkActivity?.(\"received\", message);\n\n if (callbacks.onMessage) {\n callbacks.onMessage(message);\n }\n });\n\n this.#webSocket.on(\"error\", (event: Error) => {\n this.#socketReady = false;\n if (callbacks.onError) {\n callbacks.onError(event);\n }\n });\n\n this.#webSocket.on(\"close\", (event: unknown) => {\n this.#socketReady = false;\n if (callbacks.onClose) {\n callbacks.onClose(event);\n }\n });\n }\n\n #drain(): void {\n if (\n !this.#socketReady ||\n !this.isConnectionOpen() ||\n this.#outboundQueue.length === 0\n ) {\n return;\n }\n\n while (this.#outboundQueue.length > 0 && this.isConnectionOpen()) {\n const item = this.#outboundQueue.shift();\n\n if (item === undefined) {\n break;\n }\n\n if (item instanceof Blob) {\n this.#webSocket!.send(item);\n this.#callbacks?.onNetworkActivity?.(\"sent\", {\n size: item.size,\n type: \"audio\",\n });\n continue;\n }\n\n this.#webSocket!.send(JSON.stringify(item));\n this.#callbacks?.onNetworkActivity?.(\"sent\", {\n type: item.type,\n });\n }\n }\n\n mediaRecorderHandler = (data: Blob): void => {\n if (this.#socketReady && this.isConnectionOpen()) {\n this.#webSocket?.send(data);\n this.#callbacks?.onNetworkActivity?.(\"sent\", {\n size: data.size,\n type: \"audio\",\n });\n return;\n }\n\n this.#outboundQueue.push(data);\n };\n\n async pause(): Promise<void> {\n if (this.#socketReady && this.isConnectionOpen()) {\n this.#webSocket?.send(JSON.stringify({ type: \"flush\" }));\n this.#callbacks?.onNetworkActivity?.(\"sent\", { type: \"flush\" });\n return;\n }\n\n this.#outboundQueue.push({ type: \"flush\" });\n }\n\n isConnectionOpen(): boolean {\n return (\n this.#webSocket !== null &&\n (this.#webSocket.readyState === WebSocket.OPEN ||\n this.#webSocket.readyState === WebSocket.CONNECTING)\n );\n }\n\n isConnecting(): boolean {\n return this.#isConnecting;\n }\n\n async waitForConnection(): Promise<void> {\n await this.#connectingPromise;\n }\n\n async closeConnection(onClose?: (event: unknown) => void): Promise<void> {\n await new Promise<void>((resolve, reject) => {\n const oldSocket = this.#webSocket;\n this.#webSocket = null;\n\n if (\n !oldSocket ||\n (oldSocket.readyState !== WebSocket.OPEN &&\n oldSocket.readyState !== WebSocket.CONNECTING)\n ) {\n this.#socketReady = false;\n resolve();\n return;\n }\n\n oldSocket.on(\"close\", (event) => {\n if (this.#closeTimeout) {\n clearTimeout(this.#closeTimeout);\n this.#closeTimeout = undefined;\n }\n\n if (onClose) {\n onClose(event);\n }\n\n resolve();\n });\n\n const wasReady = this.#socketReady;\n this.#socketReady = false;\n\n oldSocket.on(\"message\", (message) => {\n this.#callbacks?.onNetworkActivity?.(\"received\", message);\n\n if (this.#callbacks?.onMessage) {\n this.#callbacks?.onMessage(message);\n }\n\n // closeConnection() may be called before CONFIG_ACCEPTED arrives (e.g.\n // openConnection() followed immediately by closeConnection()). We can't\n // use the outbound queue here because #webSocket is already null, so we\n // send \"end\" directly on oldSocket as soon as config is accepted.\n if (!wasReady && message.type === \"CONFIG_ACCEPTED\") {\n oldSocket.sendEnd({ type: \"end\" });\n this.#callbacks?.onNetworkActivity?.(\"sent\", { type: \"end\" });\n return;\n }\n\n if (message.type === \"ended\") {\n if (this.#closeTimeout) {\n clearTimeout(this.#closeTimeout);\n this.#closeTimeout = undefined;\n }\n\n resolve();\n return;\n }\n });\n\n if (wasReady) {\n oldSocket.sendEnd({ type: \"end\" });\n this.#callbacks?.onNetworkActivity?.(\"sent\", { type: \"end\" });\n }\n\n this.#closeTimeout = window.setTimeout(() => {\n reject(new Error(\"Connection close timeout\"));\n\n if (oldSocket?.readyState === WebSocket.OPEN) {\n oldSocket.close();\n }\n }, 10000);\n });\n }\n\n cleanup(): void {\n // Incrementing generation invalidates any in-flight #doConnect awaits,\n // causing them to discard their socket and return \"superseded\".\n this.#connectionGeneration++;\n this.#socketReady = false;\n\n if (this.#closeTimeout) {\n clearTimeout(this.#closeTimeout);\n this.#closeTimeout = undefined;\n }\n\n if (this.isConnectionOpen()) {\n this.#webSocket?.close();\n }\n\n this.#webSocket = null;\n this.#cortiClient = null;\n this.#lastDictationConfig = null;\n this.#lastSocketUrl = undefined;\n this.#lastSocketProxy = undefined;\n\n if (this.#outboundQueue.length > 0) {\n this.host.dispatchEvent(\n errorEvent(\n `${this.#outboundQueue.length} unsent audio message(s) were discarded because the configuration changed before the connection was closed`,\n ),\n );\n }\n\n this.#outboundQueue = [];\n }\n}\n"]}
|
|
@@ -9,7 +9,7 @@ export declare class MediaController implements ReactiveController {
|
|
|
9
9
|
host: MediaControllerHost;
|
|
10
10
|
constructor(host: MediaControllerHost);
|
|
11
11
|
hostDisconnected(): void;
|
|
12
|
-
initialize(onTrackEnded?: () => void): Promise<void>;
|
|
12
|
+
initialize(onTrackEnded?: () => void, dataHandler?: (data: Blob) => void): Promise<void>;
|
|
13
13
|
getAudioLevel(): number;
|
|
14
14
|
startAudioLevelMonitoring(onAudioLevelChange?: (level: number) => void): void;
|
|
15
15
|
stopAudioLevelMonitoring(): void;
|
|
@@ -19,8 +19,6 @@ export declare class MediaController implements ReactiveController {
|
|
|
19
19
|
* This ensures the final ondataavailable event fires before resolving.
|
|
20
20
|
*/
|
|
21
21
|
stopRecording(): Promise<void>;
|
|
22
|
-
addDataHandler(handler: (data: Blob) => void): void;
|
|
23
|
-
removeDataHandler(): void;
|
|
24
22
|
get mediaRecorder(): MediaRecorder | null;
|
|
25
23
|
get audioLevel(): number;
|
|
26
24
|
}
|