@ephia/dova-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -0
- package/dist/EphiaBinding-BvRmlqqC.d.ts +36 -0
- package/dist/EphiaFloatingButton-CxiF86VW.d.ts +65 -0
- package/dist/EphiaTextarea-B4_CAVUg.d.ts +183 -0
- package/dist/NativeBinding-ChG0GeSz.d.ts +53 -0
- package/dist/TargetBinding-BKGQwUMc.d.ts +89 -0
- package/dist/TiptapBinding-B-agfV2H.d.ts +45 -0
- package/dist/Transport-zdeA4Pou.d.ts +63 -0
- package/dist/audio-state-kZ3KSvux.d.ts +39 -0
- package/dist/chunk-35AJK2IO.js +1 -0
- package/dist/chunk-35AJK2IO.js.map +1 -0
- package/dist/chunk-3LXZODL4.js +886 -0
- package/dist/chunk-3LXZODL4.js.map +1 -0
- package/dist/chunk-5IK5TLSK.js +67 -0
- package/dist/chunk-5IK5TLSK.js.map +1 -0
- package/dist/chunk-7E43RY75.js +9 -0
- package/dist/chunk-7E43RY75.js.map +1 -0
- package/dist/chunk-A5UEXJ5R.js +183 -0
- package/dist/chunk-A5UEXJ5R.js.map +1 -0
- package/dist/chunk-AEE554FT.js +51 -0
- package/dist/chunk-AEE554FT.js.map +1 -0
- package/dist/chunk-DIEWY3IT.js +1332 -0
- package/dist/chunk-DIEWY3IT.js.map +1 -0
- package/dist/chunk-EGIAN7FH.js +18 -0
- package/dist/chunk-EGIAN7FH.js.map +1 -0
- package/dist/chunk-EMOEAPVU.js +486 -0
- package/dist/chunk-EMOEAPVU.js.map +1 -0
- package/dist/chunk-IDC7FHIZ.js +40 -0
- package/dist/chunk-IDC7FHIZ.js.map +1 -0
- package/dist/chunk-ITJFN3VM.js +601 -0
- package/dist/chunk-ITJFN3VM.js.map +1 -0
- package/dist/chunk-K24GNU27.js +22 -0
- package/dist/chunk-K24GNU27.js.map +1 -0
- package/dist/chunk-LXMCRXXF.js +778 -0
- package/dist/chunk-LXMCRXXF.js.map +1 -0
- package/dist/chunk-MJCEOOLW.js +122 -0
- package/dist/chunk-MJCEOOLW.js.map +1 -0
- package/dist/chunk-N7U5M3VZ.js +33 -0
- package/dist/chunk-N7U5M3VZ.js.map +1 -0
- package/dist/chunk-PSYX674B.js +27 -0
- package/dist/chunk-PSYX674B.js.map +1 -0
- package/dist/chunk-RFQRV7ML.js +33 -0
- package/dist/chunk-RFQRV7ML.js.map +1 -0
- package/dist/chunk-THNHRV2B.js +18 -0
- package/dist/chunk-THNHRV2B.js.map +1 -0
- package/dist/chunk-VSLGR64U.js +62 -0
- package/dist/chunk-VSLGR64U.js.map +1 -0
- package/dist/chunk-W2ZP674X.js +346 -0
- package/dist/chunk-W2ZP674X.js.map +1 -0
- package/dist/chunk-YWZUMUYE.js +695 -0
- package/dist/chunk-YWZUMUYE.js.map +1 -0
- package/dist/client-options-Uo6jXO8k.d.ts +64 -0
- package/dist/connection-state-Bk33YprE.d.ts +32 -0
- package/dist/core/bindings/index.d.ts +24 -0
- package/dist/core/bindings/index.js +1025 -0
- package/dist/core/bindings/index.js.map +1 -0
- package/dist/core/index.d.ts +383 -0
- package/dist/core/index.js +1284 -0
- package/dist/core/index.js.map +1 -0
- package/dist/createEphiaClient-BhdZ183V.d.ts +69 -0
- package/dist/devices/speechmike/index.d.ts +148 -0
- package/dist/devices/speechmike/index.js +40 -0
- package/dist/devices/speechmike/index.js.map +1 -0
- package/dist/headless/index.d.ts +10 -0
- package/dist/headless/index.js +25 -0
- package/dist/headless/index.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +119 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +38 -0
- package/dist/react/index.js +70 -0
- package/dist/react/index.js.map +1 -0
- package/dist/rich-editor/index.d.ts +46 -0
- package/dist/rich-editor/index.js +13 -0
- package/dist/rich-editor/index.js.map +1 -0
- package/dist/schema-B2ycPlNB.d.ts +87 -0
- package/dist/session-APaXR48R.d.ts +12 -0
- package/dist/shared/index.d.ts +16 -0
- package/dist/shared/index.js +30 -0
- package/dist/shared/index.js.map +1 -0
- package/dist/style.css +1093 -0
- package/dist/testing/index.d.ts +84 -0
- package/dist/testing/index.js +36 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/types-D5SXPSwR.d.ts +32 -0
- package/dist/ui/index.d.ts +30 -0
- package/dist/ui/index.js +34 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/useEphiaSpeechMike-CjD7DWnh.d.ts +64 -0
- package/package.json +110 -0
- package/src/core/audio/audio-worklet-source.ts +30 -0
- package/src/core/audio/index.ts +3 -0
- package/src/core/audio/voice-level-meter.test.ts +27 -0
- package/src/core/audio/voice-level-meter.ts +270 -0
- package/src/core/bindings/EphiaBinding.ts +41 -0
- package/src/core/bindings/SegmentBindingBridge.test.ts +422 -0
- package/src/core/bindings/SegmentBindingBridge.ts +377 -0
- package/src/core/bindings/TargetBinding.ts +142 -0
- package/src/core/bindings/adapters/NativeAdapter.test.ts +85 -0
- package/src/core/bindings/adapters/NativeAdapter.ts +216 -0
- package/src/core/bindings/adapters/ProseMirrorAdapter.ts +231 -0
- package/src/core/bindings/adapters/index.ts +2 -0
- package/src/core/bindings/binding-factory.ts +78 -0
- package/src/core/bindings/detect-editor-type.ts +87 -0
- package/src/core/bindings/index.ts +13 -0
- package/src/core/bindings/insertion-boundary.test.ts +38 -0
- package/src/core/bindings/insertion-boundary.ts +26 -0
- package/src/core/bindings/native/NativeBinding.test.ts +277 -0
- package/src/core/bindings/native/NativeBinding.ts +239 -0
- package/src/core/bindings/resolver.ts +18 -0
- package/src/core/bindings/targets/codemirror.binding.ts +293 -0
- package/src/core/bindings/targets/contenteditable.binding.ts +452 -0
- package/src/core/bindings/targets/index.ts +10 -0
- package/src/core/bindings/targets/monaco.binding.ts +315 -0
- package/src/core/bindings/targets/tiptap.binding.test.ts +417 -0
- package/src/core/bindings/targets/tiptap.binding.ts +1192 -0
- package/src/core/bindings/tiptap/TiptapBinding.test.ts +63 -0
- package/src/core/bindings/tiptap/TiptapBinding.ts +464 -0
- package/src/core/bindings/types.ts +41 -0
- package/src/core/client/EphiaAudioClient.ts +654 -0
- package/src/core/client/audio-capture.ts +263 -0
- package/src/core/client/client-options.ts +39 -0
- package/src/core/client/client-state.ts +18 -0
- package/src/core/client/constants.ts +23 -0
- package/src/core/client/session-api.ts +415 -0
- package/src/core/connection/connection-state.ts +78 -0
- package/src/core/connection/index.ts +6 -0
- package/src/core/index.ts +47 -0
- package/src/core/operations/textToDocumentOperations.test.ts +69 -0
- package/src/core/operations/textToDocumentOperations.ts +92 -0
- package/src/core/runtime/DictationRuntime.test.ts +578 -0
- package/src/core/runtime/DictationRuntime.ts +434 -0
- package/src/core/runtime/TranscriptApplier.test.ts +355 -0
- package/src/core/runtime/TranscriptApplier.ts +229 -0
- package/src/core/runtime/index.ts +18 -0
- package/src/core/session/index.ts +2 -0
- package/src/core/session/session-machine.test.ts +16 -0
- package/src/core/session/session-machine.ts +59 -0
- package/src/core/targets/EditorContextCollector.ts +71 -0
- package/src/core/targets/TargetManager.test.ts +194 -0
- package/src/core/targets/TargetManager.ts +194 -0
- package/src/core/targets/index.ts +10 -0
- package/src/core/text-processing/index.ts +11 -0
- package/src/core/text-processing/overlap.test.ts +35 -0
- package/src/core/text-processing/overlap.ts +101 -0
- package/src/core/text-processing/voice-formatting.normalizer.test.ts +132 -0
- package/src/core/text-processing/voice-formatting.normalizer.ts +284 -0
- package/src/core/transcript/client-transcript.reducer.ts +366 -0
- package/src/core/transcript/client-transcript.state.ts +25 -0
- package/src/core/transcript/index.ts +19 -0
- package/src/core/transcript/transcript.assembler.test.ts +205 -0
- package/src/core/transcript/transcript.assembler.ts +152 -0
- package/src/core/transcript/transcript.reducer.test.ts +199 -0
- package/src/core/transcript/transcript.reducer.ts +771 -0
- package/src/core/transcript/transcript.state.ts +123 -0
- package/src/core/transport/LiveKitTransport.publish.test.ts +226 -0
- package/src/core/transport/LiveKitTransport.ts +459 -0
- package/src/core/transport/MockTransport.ts +231 -0
- package/src/core/transport/Transport.ts +82 -0
- package/src/debug/sdk-debug-collector.ts +79 -0
- package/src/devices/index.ts +2 -0
- package/src/devices/speechmike/__tests__/EphiaSpeechMikeProvider.test.tsx +99 -0
- package/src/devices/speechmike/__tests__/speechmike-audio-resolver.test.ts +96 -0
- package/src/devices/speechmike/__tests__/speechmike-button-router.test.ts +66 -0
- package/src/devices/speechmike/__tests__/speechmike-device-manager.test.ts +201 -0
- package/src/devices/speechmike/__tests__/speechmike-led-controller.test.ts +68 -0
- package/src/devices/speechmike/browser.ts +80 -0
- package/src/devices/speechmike/constants.ts +74 -0
- package/src/devices/speechmike/dictation-support-loader.ts +81 -0
- package/src/devices/speechmike/index.ts +11 -0
- package/src/devices/speechmike/react/EphiaSpeechMikeContext.ts +34 -0
- package/src/devices/speechmike/react/EphiaSpeechMikeProvider.tsx +287 -0
- package/src/devices/speechmike/react/useEphiaSpeechMike.ts +26 -0
- package/src/devices/speechmike/speechmike-audio-resolver.ts +58 -0
- package/src/devices/speechmike/speechmike-button-router.ts +73 -0
- package/src/devices/speechmike/speechmike-device-manager.ts +461 -0
- package/src/devices/speechmike/speechmike-led-controller.ts +78 -0
- package/src/devices/speechmike/types.ts +96 -0
- package/src/dictation_support.d.ts +31 -0
- package/src/global.d.ts +10 -0
- package/src/headless/createEphiaClient.ts +220 -0
- package/src/headless/index.ts +18 -0
- package/src/index.ts +89 -0
- package/src/react/EphiaAuto.tsx +87 -0
- package/src/react/components/EphiaDictationButton.tsx +88 -0
- package/src/react/components/EphiaStatusBar.tsx +59 -0
- package/src/react/components/EphiaTextarea.tsx +295 -0
- package/src/react/ephia-react.css +318 -0
- package/src/react/hooks/targets/index.ts +3 -0
- package/src/react/hooks/targets/useEphiaCodemirror.ts +35 -0
- package/src/react/hooks/targets/useEphiaMonaco.ts +35 -0
- package/src/react/hooks/targets/useEphiaTiptap.ts +23 -0
- package/src/react/hooks/useEphia.lifecycle.test.tsx +389 -0
- package/src/react/hooks/useEphia.ts +367 -0
- package/src/react/hooks/useEphiaDiscardTarget.ts +53 -0
- package/src/react/hooks/useEphiaServerEvent.ts +33 -0
- package/src/react/hooks/useEphiaTarget.ts +47 -0
- package/src/react/hooks/useEphiaTranscript.ts +22 -0
- package/src/react/index.ts +58 -0
- package/src/react/provider/EphiaContext.ts +63 -0
- package/src/react/provider/EphiaInternalContext.ts +32 -0
- package/src/react/provider/EphiaProvider.tsx +373 -0
- package/src/react/registry/binding-factory.ts +7 -0
- package/src/react/registry/detect-editor-type.ts +2 -0
- package/src/react/registry/events.ts +37 -0
- package/src/react/registry/registries/CodeMirrorInstanceRegistry.ts +24 -0
- package/src/react/registry/registries/MonacoInstanceRegistry.ts +23 -0
- package/src/react/registry/registries/TargetRegistry.ts +327 -0
- package/src/react/registry/registries/TiptapInstanceRegistry.ts +43 -0
- package/src/react/registry/registries/index.ts +5 -0
- package/src/react/store/create-ephia-store.ts +36 -0
- package/src/react/store/types.ts +41 -0
- package/src/react/utils/flash-range.ts +24 -0
- package/src/react/utils/index.ts +1 -0
- package/src/rich-editor/adapters/tiptap.test.ts +86 -0
- package/src/rich-editor/adapters/tiptap.ts +23 -0
- package/src/rich-editor/index.ts +3 -0
- package/src/rich-editor/types.ts +24 -0
- package/src/rich-editor/use-ephia-rich-editor.test.tsx +202 -0
- package/src/rich-editor/use-ephia-rich-editor.ts +47 -0
- package/src/shared/config/endpoint.test.ts +45 -0
- package/src/shared/config/endpoint.ts +39 -0
- package/src/shared/config/schema.ts +32 -0
- package/src/shared/effective-text.ts +13 -0
- package/src/shared/errors/EphiaSdkError.ts +54 -0
- package/src/shared/errors/messages.ts +40 -0
- package/src/shared/index.ts +27 -0
- package/src/shared/state/audio-state.ts +45 -0
- package/src/shared/state/index.ts +2 -0
- package/src/shared/store/document-store.ts +32 -0
- package/src/shared/store/index.ts +2 -0
- package/src/shared/types/editors.ts +28 -0
- package/src/shared/types/session.ts +12 -0
- package/src/style.css +2 -0
- package/src/testing/index.tsx +60 -0
- package/src/ui/assets/ephia-logo.svg +4 -0
- package/src/ui/components/EphiaLogo.tsx +77 -0
- package/src/ui/index.ts +24 -0
- package/src/ui/primitives/Button.tsx +53 -0
- package/src/ui/primitives/Spinner.tsx +21 -0
- package/src/ui/primitives/index.ts +5 -0
- package/src/ui/recorder/EphiaFloatingButton.tsx +489 -0
- package/src/ui/recorder/MinimalProcessingBars.tsx +122 -0
- package/src/ui/recorder/StandardIntensityVisualizer.tsx +148 -0
- package/src/ui/recorder/appearance.ts +9 -0
- package/src/ui/recorder/index.ts +8 -0
- package/src/ui/theme.css +775 -0
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EphiaAudioClient — façade publique vers le nouvel agent ephia-transcribe-agent.
|
|
3
|
+
*
|
|
4
|
+
* Architecture simplifiée : SessionApiClient + AudioCaptureManager + Transport direct.
|
|
5
|
+
* Pas d'EventGate / CatchupCoordinator / SessionSyncService / ReconnectionManager.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { EphiaClientMessage, EphiaServerEvent } from "ephia-protocol";
|
|
9
|
+
import type { Transport, TransportState } from "../transport/Transport";
|
|
10
|
+
import { LiveKitTransport } from "../transport/LiveKitTransport";
|
|
11
|
+
import type { EphiaClientState } from "./client-state";
|
|
12
|
+
import { initialClientState } from "./client-state";
|
|
13
|
+
import { canTransition, transitionSessionStatus } from "../session/session-machine";
|
|
14
|
+
import type { EphiaSessionStatus } from "../session/session-machine";
|
|
15
|
+
import { EphiaSdkError } from "../../shared/errors/EphiaSdkError";
|
|
16
|
+
import { dbgState } from "../../debug/sdk-debug-collector";
|
|
17
|
+
import { SDK_VERSION, PROTOCOL_VERSION } from "./constants";
|
|
18
|
+
import type { EphiaAudioClientOptions, EphiaStartOptions, EphiaStartResult } from "./client-options";
|
|
19
|
+
export type { EphiaAudioClientOptions, EphiaStartOptions, EphiaStartResult } from "./client-options";
|
|
20
|
+
export { SDK_VERSION, PROTOCOL_VERSION } from "./constants";
|
|
21
|
+
import { SessionApiClient } from "./session-api";
|
|
22
|
+
import { AudioCaptureManager } from "./audio-capture";
|
|
23
|
+
|
|
24
|
+
export class EphiaAudioClient {
|
|
25
|
+
private options: EphiaAudioClientOptions;
|
|
26
|
+
private transport: Transport;
|
|
27
|
+
private state: EphiaClientState = { ...initialClientState };
|
|
28
|
+
private _unbindTransport?: () => void;
|
|
29
|
+
|
|
30
|
+
private _stopping = false;
|
|
31
|
+
private _intentionalDisconnect = false;
|
|
32
|
+
private _startAbortController: AbortController | null = null;
|
|
33
|
+
private _lastConnectParams: { livekitUrl: string; token: string; roomName: string; sessionId: string } | null = null;
|
|
34
|
+
private _tokenRefreshTimer: ReturnType<typeof setTimeout> | null = null;
|
|
35
|
+
private _serverSessionClosed = false;
|
|
36
|
+
|
|
37
|
+
private readonly sessionApi: SessionApiClient;
|
|
38
|
+
private readonly audio: AudioCaptureManager;
|
|
39
|
+
|
|
40
|
+
constructor(options: EphiaAudioClientOptions = {}) {
|
|
41
|
+
this.options = options;
|
|
42
|
+
this.transport = options.transport ?? new LiveKitTransport();
|
|
43
|
+
|
|
44
|
+
this.sessionApi = new SessionApiClient({
|
|
45
|
+
apiUrl: options.apiUrl,
|
|
46
|
+
apiKey: options.apiKey,
|
|
47
|
+
bearerToken: options.bearerToken,
|
|
48
|
+
clientType: options.clientType,
|
|
49
|
+
sessionOptions: options.sessionOptions,
|
|
50
|
+
isIdle: () => this.state.status === "idle",
|
|
51
|
+
onPrepareConnection: (url, token) => {
|
|
52
|
+
this.transport.prepareConnection?.(url, token).catch(() => {});
|
|
53
|
+
},
|
|
54
|
+
onPreloadComplete: () => {
|
|
55
|
+
this._emitSdkDebug("sdk.preload.done", { ts: Date.now() });
|
|
56
|
+
},
|
|
57
|
+
onPreloadFailed: () => {
|
|
58
|
+
this._emitSdkDebug("sdk.preload.failed", { ts: Date.now() });
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
this.audio = new AudioCaptureManager({
|
|
63
|
+
onAudioState: (s) => this.options.onAudioState?.(s),
|
|
64
|
+
getLocalAudioPublished: () => this.state.isMicEnabled,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ── Public API ──────────────────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
async preload(signal?: AbortSignal): Promise<void>;
|
|
71
|
+
async preload(options?: EphiaStartOptions, signal?: AbortSignal): Promise<void>;
|
|
72
|
+
async preload(
|
|
73
|
+
optionsOrSignal?: EphiaStartOptions | AbortSignal,
|
|
74
|
+
maybeSignal?: AbortSignal
|
|
75
|
+
): Promise<void> {
|
|
76
|
+
if (this.state.status !== "idle") return;
|
|
77
|
+
if (optionsOrSignal && "aborted" in optionsOrSignal) {
|
|
78
|
+
return this.sessionApi.preload(optionsOrSignal);
|
|
79
|
+
}
|
|
80
|
+
return this.sessionApi.preload(optionsOrSignal, maybeSignal);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
releasePreloadedSession(): void {
|
|
84
|
+
this.sessionApi.cancelTimers();
|
|
85
|
+
const orphaned = this.sessionApi.discardPreloadedSession();
|
|
86
|
+
if (orphaned) {
|
|
87
|
+
this.sessionApi.stopBackendSession(orphaned.sessionId).catch(() => {});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async warmupMic(): Promise<void> {
|
|
92
|
+
return this.audio.warmupMic();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
injectWarmupStream(stream: MediaStream): void {
|
|
96
|
+
this.audio.injectWarmupStream(stream);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async start(options?: EphiaStartOptions): Promise<EphiaStartResult> {
|
|
100
|
+
if (this.state.status === "ended" || this.state.status === "error") {
|
|
101
|
+
this._resetForStart();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Resume from paused (room still connected, just republish audio)
|
|
105
|
+
if (this.state.status === "paused" && this._hasReusableRoom()) {
|
|
106
|
+
await this._resumeAudio(options);
|
|
107
|
+
return { sessionId: this.state.sessionId };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (!canTransition(this.state.status, "creating_session")) {
|
|
111
|
+
throw new EphiaSdkError(
|
|
112
|
+
"client.invalid_state",
|
|
113
|
+
`Cannot start while status is ${this.state.status}`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
this._setStatus("creating_session");
|
|
118
|
+
this._startAbortController = new AbortController();
|
|
119
|
+
const { signal } = this._startAbortController;
|
|
120
|
+
|
|
121
|
+
let acquiredStream: MediaStream | undefined;
|
|
122
|
+
let sessionData: Awaited<ReturnType<SessionApiClient["createSession"]>> | undefined;
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
// Acquire mic + session in parallel
|
|
126
|
+
const micPromise = this.audio
|
|
127
|
+
.acquireMicStream(this.options.preferredAudioInputDeviceId)
|
|
128
|
+
.then((stream) => {
|
|
129
|
+
if (!signal.aborted) {
|
|
130
|
+
acquiredStream = stream;
|
|
131
|
+
this.audio.setActiveStream(stream);
|
|
132
|
+
} else {
|
|
133
|
+
stream.getTracks().forEach((t) => t.stop());
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
let sessionPromise: Promise<void>;
|
|
138
|
+
const preloaded = this.sessionApi.takePreloadedSession();
|
|
139
|
+
if (preloaded) {
|
|
140
|
+
sessionData = preloaded;
|
|
141
|
+
this.state = { ...this.state, sessionId: preloaded.sessionId, roomName: preloaded.roomName };
|
|
142
|
+
this.options.onStateChange?.(this.getState());
|
|
143
|
+
sessionPromise = Promise.resolve();
|
|
144
|
+
} else {
|
|
145
|
+
if (this.sessionApi.hasPreloadInFlight()) {
|
|
146
|
+
await this.sessionApi.awaitPreloadIfPending(signal);
|
|
147
|
+
const fromFlight = this.sessionApi.takePreloadedSession();
|
|
148
|
+
if (fromFlight) {
|
|
149
|
+
sessionData = fromFlight;
|
|
150
|
+
this.state = { ...this.state, sessionId: fromFlight.sessionId, roomName: fromFlight.roomName };
|
|
151
|
+
this.options.onStateChange?.(this.getState());
|
|
152
|
+
sessionPromise = Promise.resolve();
|
|
153
|
+
} else {
|
|
154
|
+
sessionPromise = this.sessionApi.createSessionWithRetry(signal, options).then((data) => {
|
|
155
|
+
if (!signal.aborted) {
|
|
156
|
+
sessionData = data;
|
|
157
|
+
this.state = { ...this.state, sessionId: data.sessionId, roomName: data.roomName };
|
|
158
|
+
this.options.onStateChange?.(this.getState());
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
sessionPromise = this.sessionApi.createSessionWithRetry(signal, options).then((data) => {
|
|
164
|
+
if (!signal.aborted) {
|
|
165
|
+
sessionData = data;
|
|
166
|
+
this.state = { ...this.state, sessionId: data.sessionId, roomName: data.roomName };
|
|
167
|
+
this.options.onStateChange?.(this.getState());
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
await Promise.all([micPromise, sessionPromise]);
|
|
174
|
+
|
|
175
|
+
if (signal.aborted) {
|
|
176
|
+
this._handleStartAborted();
|
|
177
|
+
return { sessionId: null };
|
|
178
|
+
}
|
|
179
|
+
if (!acquiredStream || !sessionData) {
|
|
180
|
+
throw new EphiaSdkError("client.start_failed", "Missing stream or session");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Connect transport
|
|
184
|
+
this._setStatus("connecting_transport");
|
|
185
|
+
this._unbindTransport?.();
|
|
186
|
+
this._unbindTransport = this._bindTransportEvents();
|
|
187
|
+
|
|
188
|
+
const connectParams = {
|
|
189
|
+
livekitUrl: sessionData.livekitUrl,
|
|
190
|
+
token: sessionData.token,
|
|
191
|
+
roomName: sessionData.roomName,
|
|
192
|
+
sessionId: sessionData.sessionId,
|
|
193
|
+
};
|
|
194
|
+
this._lastConnectParams = connectParams;
|
|
195
|
+
this._scheduleTokenRefresh(sessionData.sessionId);
|
|
196
|
+
await this._connectTransportWithRetry(connectParams, signal);
|
|
197
|
+
await this._sendInitialTargetBeforeAudio(options?.initialTargetId, signal);
|
|
198
|
+
|
|
199
|
+
if (signal.aborted) {
|
|
200
|
+
await this._disconnectTransport().catch(() => {});
|
|
201
|
+
this._handleStartAborted();
|
|
202
|
+
return { sessionId: null };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Publish audio
|
|
206
|
+
this._setStatus("ready");
|
|
207
|
+
const track = acquiredStream.getAudioTracks()[0];
|
|
208
|
+
if (!track) {
|
|
209
|
+
throw new EphiaSdkError("audio.no_input_device", "No audio track");
|
|
210
|
+
}
|
|
211
|
+
if (!track.enabled) track.enabled = true;
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
await this.transport.publishAudio(track, { enableNoiseFilter: this.options.noiseFilter });
|
|
215
|
+
} catch (err) {
|
|
216
|
+
if (err instanceof EphiaSdkError) throw err;
|
|
217
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
218
|
+
throw new EphiaSdkError("audio.track_publish_failed", message);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (signal.aborted) {
|
|
222
|
+
await this.transport.unpublishAudio().catch(() => {});
|
|
223
|
+
await this._disconnectTransport().catch(() => {});
|
|
224
|
+
this._handleStartAborted();
|
|
225
|
+
return { sessionId: null };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
this._setStatus("recording");
|
|
229
|
+
this.state = { ...this.state, isMicEnabled: true };
|
|
230
|
+
this.options.onStateChange?.(this.getState());
|
|
231
|
+
await this.audio.setupAudioLevel(acquiredStream);
|
|
232
|
+
|
|
233
|
+
this._emitSdkDebug("sdk.record.started", { sessionId: sessionData.sessionId, ts: Date.now() });
|
|
234
|
+
|
|
235
|
+
return { sessionId: sessionData.sessionId };
|
|
236
|
+
} catch (err) {
|
|
237
|
+
if (signal.aborted) {
|
|
238
|
+
await this._disconnectTransport().catch(() => {});
|
|
239
|
+
this._handleStartAborted();
|
|
240
|
+
return { sessionId: null };
|
|
241
|
+
}
|
|
242
|
+
const code = err instanceof EphiaSdkError ? err.code : "client.start_failed";
|
|
243
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
244
|
+
this._setErrorState(code, message);
|
|
245
|
+
this.audio.cleanupActiveStream();
|
|
246
|
+
await this._disconnectTransport().catch(() => {});
|
|
247
|
+
throw err;
|
|
248
|
+
} finally {
|
|
249
|
+
if (this._startAbortController?.signal === signal) {
|
|
250
|
+
this._startAbortController = null;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Arrête l'enregistrement sans fermer la room (reprise possible via start()). */
|
|
256
|
+
async stop(): Promise<void> {
|
|
257
|
+
if (this._stopping) return;
|
|
258
|
+
this.audio.stopAudioLevelImmediately?.();
|
|
259
|
+
|
|
260
|
+
if (this._startAbortController) {
|
|
261
|
+
this._stopping = true;
|
|
262
|
+
this._startAbortController.abort();
|
|
263
|
+
this._startAbortController = null;
|
|
264
|
+
this._handleStartAborted();
|
|
265
|
+
this._stopping = false;
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (
|
|
270
|
+
this.state.status === "idle" ||
|
|
271
|
+
this.state.status === "paused" ||
|
|
272
|
+
this.state.status === "ended" ||
|
|
273
|
+
this.state.status === "error" ||
|
|
274
|
+
this.state.status === "disposed"
|
|
275
|
+
) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (
|
|
280
|
+
this.state.status === "creating_session" ||
|
|
281
|
+
this.state.status === "connecting_transport"
|
|
282
|
+
) {
|
|
283
|
+
this._stopping = true;
|
|
284
|
+
await this._disconnectTransport().catch(() => {});
|
|
285
|
+
this._handleStartAborted();
|
|
286
|
+
this._stopping = false;
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
this._stopping = true;
|
|
291
|
+
try {
|
|
292
|
+
await this.transport.unpublishAudio().catch(() => {});
|
|
293
|
+
this.audio.cleanupActiveStream();
|
|
294
|
+
this.state = { ...this.state, isMicEnabled: false };
|
|
295
|
+
this._setStatus("paused");
|
|
296
|
+
this.options.onStateChange?.(this.getState());
|
|
297
|
+
} finally {
|
|
298
|
+
this._stopping = false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Termine complètement la session : déconnecte la room et stop le backend. */
|
|
303
|
+
async endSession(): Promise<void> {
|
|
304
|
+
if (this._stopping) return;
|
|
305
|
+
this.audio.stopAudioLevelImmediately?.();
|
|
306
|
+
|
|
307
|
+
if (this._startAbortController) {
|
|
308
|
+
this._stopping = true;
|
|
309
|
+
this._startAbortController.abort();
|
|
310
|
+
this._startAbortController = null;
|
|
311
|
+
this._handleStartAborted();
|
|
312
|
+
this._stopping = false;
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (
|
|
317
|
+
this.state.status === "idle" ||
|
|
318
|
+
this.state.status === "ended" ||
|
|
319
|
+
this.state.status === "disposed"
|
|
320
|
+
) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
this._stopping = true;
|
|
325
|
+
const sessionId = this.state.sessionId;
|
|
326
|
+
try {
|
|
327
|
+
await this.transport.unpublishAudio().catch(() => {});
|
|
328
|
+
this.audio.cleanupActiveStream();
|
|
329
|
+
this._intentionalDisconnect = true;
|
|
330
|
+
await this._disconnectTransport().catch(() => {});
|
|
331
|
+
if (sessionId) {
|
|
332
|
+
this.sessionApi.stopBackendSession(sessionId).catch(() => {});
|
|
333
|
+
}
|
|
334
|
+
this._markSessionEnded();
|
|
335
|
+
} finally {
|
|
336
|
+
this._intentionalDisconnect = false;
|
|
337
|
+
this._stopping = false;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
registerPageHideStop(): () => void {
|
|
342
|
+
if (typeof window === "undefined") return () => {};
|
|
343
|
+
const handler = () => {
|
|
344
|
+
const sessionId = this.state.sessionId;
|
|
345
|
+
if (sessionId) this.sessionApi.stopBackendSessionOnPageHide(sessionId);
|
|
346
|
+
};
|
|
347
|
+
window.addEventListener("pagehide", handler);
|
|
348
|
+
return () => window.removeEventListener("pagehide", handler);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async dispose(): Promise<void> {
|
|
352
|
+
if (this._startAbortController) {
|
|
353
|
+
this._startAbortController.abort();
|
|
354
|
+
this._startAbortController = null;
|
|
355
|
+
this._handleStartAborted();
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
const status = this.state.status;
|
|
359
|
+
if (status === "recording" || status === "ready") {
|
|
360
|
+
await this.endSession();
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
this.sessionApi.cancelTimers();
|
|
364
|
+
const orphaned = this.sessionApi.discardPreloadedSession();
|
|
365
|
+
if (orphaned) this.sessionApi.stopBackendSession(orphaned.sessionId).catch(() => {});
|
|
366
|
+
this._intentionalDisconnect = true;
|
|
367
|
+
await this._disconnectTransport().catch(() => {});
|
|
368
|
+
this._intentionalDisconnect = false;
|
|
369
|
+
this._unbindTransport?.();
|
|
370
|
+
this._unbindTransport = undefined;
|
|
371
|
+
this.audio.cleanupActiveStream();
|
|
372
|
+
this.audio.cleanupWarmupStream?.();
|
|
373
|
+
this.state = { ...this.state, status: "disposed" };
|
|
374
|
+
this.options.onStateChange?.(this.getState());
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
getState(): EphiaClientState {
|
|
378
|
+
return { ...this.state };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
setPreferredAudioInputDeviceId(deviceId: string | undefined): void {
|
|
382
|
+
this.options = { ...this.options, preferredAudioInputDeviceId: deviceId };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
onTransportState(callback: (state: TransportState) => void): () => void {
|
|
386
|
+
return this.transport.onTransportState(callback);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
onServerEvent(callback: (event: EphiaServerEvent) => void): () => void {
|
|
390
|
+
return this.transport.onServerEvent(callback);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async sendMessage(message: EphiaClientMessage): Promise<void> {
|
|
394
|
+
await this.transport.sendMessage(message);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
async getAvailableMicrophones(): Promise<MediaDeviceInfo[]> {
|
|
398
|
+
return this.audio.getAvailableMicrophones();
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async switchMicrophone(deviceId: string): Promise<void> {
|
|
402
|
+
await this.transport.switchActiveDevice?.("audioinput", deviceId);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
exportTrace(): Record<string, unknown> {
|
|
406
|
+
return {
|
|
407
|
+
sdkVersion: SDK_VERSION,
|
|
408
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
409
|
+
sessionId: this.state.sessionId,
|
|
410
|
+
state: this.state,
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
getDebugSnapshot(): Record<string, unknown> {
|
|
415
|
+
return {
|
|
416
|
+
sdkVersion: SDK_VERSION,
|
|
417
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
418
|
+
state: this.state,
|
|
419
|
+
lastAudioState: this.audio.lastAudioState,
|
|
420
|
+
stopping: this._stopping,
|
|
421
|
+
intentionalDisconnect: this._intentionalDisconnect,
|
|
422
|
+
preloadedSession: this.sessionApi.preloadedSession,
|
|
423
|
+
lastConnectParams: this._lastConnectParams
|
|
424
|
+
? { ...this._lastConnectParams, token: "[redacted]" }
|
|
425
|
+
: null,
|
|
426
|
+
hasActiveStream: this.audio.hasActiveStream,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// ── Internals ────────────────────────────────────────────────────────────────
|
|
431
|
+
|
|
432
|
+
private _bindTransportEvents(): () => void {
|
|
433
|
+
const unsubServerEvent = this.transport.onServerEvent((event) => {
|
|
434
|
+
if (event.type === "session.error") {
|
|
435
|
+
this._setErrorState(event.payload.code, event.payload.message);
|
|
436
|
+
this._serverSessionClosed = true;
|
|
437
|
+
}
|
|
438
|
+
if (event.type === "session.status") {
|
|
439
|
+
if (event.payload.status === "closed") {
|
|
440
|
+
this._serverSessionClosed = true;
|
|
441
|
+
} else if (event.payload.status === "ready") {
|
|
442
|
+
this._serverSessionClosed = false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
const unsubState = this.transport.onTransportState((tState) => {
|
|
448
|
+
if (tState.status === "disconnected" && !this._intentionalDisconnect && !this._stopping) {
|
|
449
|
+
// LiveKit exhausted its reconnect retries
|
|
450
|
+
this._setErrorState("transport.disconnected", "Connexion perdue");
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const unsubError = this.transport.onError((err) => {
|
|
455
|
+
this._setErrorState(err.code, err.message);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
return () => {
|
|
459
|
+
unsubServerEvent();
|
|
460
|
+
unsubState();
|
|
461
|
+
unsubError();
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
private async _resumeAudio(options?: EphiaStartOptions): Promise<void> {
|
|
466
|
+
if (this.state.isMicEnabled) return;
|
|
467
|
+
try {
|
|
468
|
+
const stream = await this.audio.acquireMicStream(this.options.preferredAudioInputDeviceId);
|
|
469
|
+
if (!stream) throw new EphiaSdkError("audio.no_input_device", "No mic stream");
|
|
470
|
+
this.audio.setActiveStream(stream);
|
|
471
|
+
const track = stream.getAudioTracks()[0];
|
|
472
|
+
if (!track) throw new EphiaSdkError("audio.no_input_device", "No audio track");
|
|
473
|
+
await this._sendInitialTargetBeforeAudio(options?.initialTargetId);
|
|
474
|
+
try {
|
|
475
|
+
await this.transport.publishAudio(track, { enableNoiseFilter: this.options.noiseFilter });
|
|
476
|
+
} catch (err) {
|
|
477
|
+
if (err instanceof EphiaSdkError) throw err;
|
|
478
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
479
|
+
throw new EphiaSdkError("audio.track_publish_failed", message);
|
|
480
|
+
}
|
|
481
|
+
await this.transport.sendMessage({
|
|
482
|
+
type: "session.resume",
|
|
483
|
+
payload: {
|
|
484
|
+
context: options?.context,
|
|
485
|
+
},
|
|
486
|
+
});
|
|
487
|
+
this._setStatus("recording");
|
|
488
|
+
this.state = { ...this.state, isMicEnabled: true };
|
|
489
|
+
this.options.onStateChange?.(this.getState());
|
|
490
|
+
await this.audio.setupAudioLevel(stream);
|
|
491
|
+
} catch (err) {
|
|
492
|
+
const code = err instanceof EphiaSdkError ? err.code : "client.start_failed";
|
|
493
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
494
|
+
this._setErrorState(code, message);
|
|
495
|
+
this.audio.cleanupActiveStream();
|
|
496
|
+
throw err;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
private _hasReusableRoom(): boolean {
|
|
501
|
+
const status = this.transport.getState().status;
|
|
502
|
+
return (
|
|
503
|
+
!!this.state.sessionId &&
|
|
504
|
+
(status === "connected" || status === "reconnected")
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
private _handleStartAborted(): void {
|
|
509
|
+
this.audio.cleanupActiveStream();
|
|
510
|
+
const orphaned = this.sessionApi.discardPreloadedSession();
|
|
511
|
+
if (orphaned) this.sessionApi.stopBackendSession(orphaned.sessionId).catch(() => {});
|
|
512
|
+
this._forceIdle();
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
private _forceIdle(): void {
|
|
516
|
+
this.state = { ...this.state, status: "idle", isMicEnabled: false };
|
|
517
|
+
this.options.onStateChange?.(this.getState());
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
private _resetForStart(): void {
|
|
521
|
+
this._unbindTransport?.();
|
|
522
|
+
this._unbindTransport = undefined;
|
|
523
|
+
this.audio.cleanupActiveStream();
|
|
524
|
+
const orphaned = this.sessionApi.discardPreloadedSession();
|
|
525
|
+
if (orphaned) this.sessionApi.stopBackendSession(orphaned.sessionId).catch(() => {});
|
|
526
|
+
this.sessionApi.cancelTimers();
|
|
527
|
+
if (this._tokenRefreshTimer !== null) {
|
|
528
|
+
clearTimeout(this._tokenRefreshTimer);
|
|
529
|
+
this._tokenRefreshTimer = null;
|
|
530
|
+
}
|
|
531
|
+
this._lastConnectParams = null;
|
|
532
|
+
this.state = {
|
|
533
|
+
...this.state,
|
|
534
|
+
status: "idle",
|
|
535
|
+
sessionId: null,
|
|
536
|
+
roomName: null,
|
|
537
|
+
error: null,
|
|
538
|
+
isMicEnabled: false,
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
private _markSessionEnded(): void {
|
|
543
|
+
this._unbindTransport?.();
|
|
544
|
+
this._unbindTransport = undefined;
|
|
545
|
+
if (this._tokenRefreshTimer !== null) {
|
|
546
|
+
clearTimeout(this._tokenRefreshTimer);
|
|
547
|
+
this._tokenRefreshTimer = null;
|
|
548
|
+
}
|
|
549
|
+
this._lastConnectParams = null;
|
|
550
|
+
if (
|
|
551
|
+
this.state.status !== "ended" &&
|
|
552
|
+
this.state.status !== "error" &&
|
|
553
|
+
this.state.status !== "disposed"
|
|
554
|
+
) {
|
|
555
|
+
this._setStatus("ended");
|
|
556
|
+
}
|
|
557
|
+
this.state = { ...this.state, isMicEnabled: false };
|
|
558
|
+
this.options.onStateChange?.(this.getState());
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
private _setStatus(next: EphiaSessionStatus): void {
|
|
562
|
+
const current = this.state.status;
|
|
563
|
+
if (current === next) return;
|
|
564
|
+
const status = transitionSessionStatus(current, next);
|
|
565
|
+
this.state = { ...this.state, status };
|
|
566
|
+
dbgState("sdk.client", this.getDebugSnapshot());
|
|
567
|
+
this.options.onStateChange?.(this.getState());
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
private _setErrorState(code: string, message: string): void {
|
|
571
|
+
const error = { code, message };
|
|
572
|
+
const current = this.state.status;
|
|
573
|
+
if (canTransition(current, "error")) {
|
|
574
|
+
this._setStatus("error");
|
|
575
|
+
} else {
|
|
576
|
+
this.state = { ...this.state, status: "error" };
|
|
577
|
+
}
|
|
578
|
+
this.state = { ...this.state, error, isMicEnabled: false };
|
|
579
|
+
this.options.onStateChange?.(this.getState());
|
|
580
|
+
this.options.onError?.(error);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
private _scheduleTokenRefresh(sessionId: string): void {
|
|
584
|
+
if (this._tokenRefreshTimer !== null) clearTimeout(this._tokenRefreshTimer);
|
|
585
|
+
this._tokenRefreshTimer = setTimeout(() => {
|
|
586
|
+
this._tokenRefreshTimer = null;
|
|
587
|
+
if (!this._lastConnectParams) return;
|
|
588
|
+
void this.sessionApi.refreshToken(sessionId).then((token) => {
|
|
589
|
+
if (token && this._lastConnectParams) {
|
|
590
|
+
this._lastConnectParams = { ...this._lastConnectParams, token };
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
}, this.sessionApi.activeTokenRefreshDelayMs);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
private async _connectTransportWithRetry(
|
|
597
|
+
params: { livekitUrl: string; token: string; roomName: string; sessionId: string },
|
|
598
|
+
signal?: AbortSignal
|
|
599
|
+
): Promise<void> {
|
|
600
|
+
const maxRetries = 2;
|
|
601
|
+
let lastErr: unknown;
|
|
602
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
603
|
+
if (signal?.aborted) throw new EphiaSdkError("client.start_failed", "Start aborted");
|
|
604
|
+
try {
|
|
605
|
+
await this.transport.connect(params as any);
|
|
606
|
+
return;
|
|
607
|
+
} catch (err) {
|
|
608
|
+
lastErr = err;
|
|
609
|
+
if (err instanceof EphiaSdkError) throw err;
|
|
610
|
+
if (signal?.aborted) throw err;
|
|
611
|
+
if (attempt < maxRetries) {
|
|
612
|
+
const delay = 300 * Math.pow(2, attempt) + Math.random() * 100;
|
|
613
|
+
await new Promise<void>((resolve) => setTimeout(resolve, delay));
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
throw lastErr;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
private async _sendInitialTargetBeforeAudio(
|
|
621
|
+
targetId?: string,
|
|
622
|
+
signal?: AbortSignal
|
|
623
|
+
): Promise<void> {
|
|
624
|
+
if (!targetId) return;
|
|
625
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
626
|
+
if (signal?.aborted) throw new EphiaSdkError("client.start_failed", "Start aborted");
|
|
627
|
+
try {
|
|
628
|
+
await this.transport.sendMessage({
|
|
629
|
+
type: "session.target.changed",
|
|
630
|
+
payload: { targetId },
|
|
631
|
+
} as unknown as EphiaClientMessage);
|
|
632
|
+
return;
|
|
633
|
+
} catch (err) {
|
|
634
|
+
if (attempt === 2) throw err;
|
|
635
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 50 * (attempt + 1)));
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
private async _disconnectTransport(): Promise<void> {
|
|
641
|
+
this._unbindTransport?.();
|
|
642
|
+
this._unbindTransport = undefined;
|
|
643
|
+
await this.transport.disconnect();
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
private _emitSdkDebug(type: string, payload: Record<string, unknown>): void {
|
|
647
|
+
if (typeof window === "undefined") return;
|
|
648
|
+
window.dispatchEvent(
|
|
649
|
+
new CustomEvent("ephia:sdk-debug", {
|
|
650
|
+
detail: { type, sessionId: this.state.sessionId ?? null, payload },
|
|
651
|
+
})
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
}
|