@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,434 @@
|
|
|
1
|
+
import type { EphiaAudioEvent } from "ephia-protocol";
|
|
2
|
+
import type { TargetBinding } from "../bindings/TargetBinding";
|
|
3
|
+
import { pickEffectiveText } from "../../shared/effective-text";
|
|
4
|
+
import {
|
|
5
|
+
normalizeCommittedText,
|
|
6
|
+
normalizePreviewText,
|
|
7
|
+
type VoiceFormattingConfig,
|
|
8
|
+
} from "../text-processing/voice-formatting.normalizer";
|
|
9
|
+
|
|
10
|
+
export type DictationTargetInsertionMode =
|
|
11
|
+
| "preview-inline"
|
|
12
|
+
| "final-only"
|
|
13
|
+
| "preview-floating"
|
|
14
|
+
| "none";
|
|
15
|
+
|
|
16
|
+
export type DictationRuntimeStatus =
|
|
17
|
+
| "idle"
|
|
18
|
+
| "recording"
|
|
19
|
+
| "processing"
|
|
20
|
+
| "error";
|
|
21
|
+
|
|
22
|
+
export interface DictationTarget {
|
|
23
|
+
id: string;
|
|
24
|
+
insertion: DictationTargetInsertionMode;
|
|
25
|
+
element: HTMLElement;
|
|
26
|
+
binding: TargetBinding | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface DictationTargetRegistry {
|
|
30
|
+
get(id: string): DictationTarget | undefined;
|
|
31
|
+
getActiveTargetId(): string | null;
|
|
32
|
+
getActiveTarget(): DictationTarget | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface DictationPartialState {
|
|
36
|
+
targetId: string | null;
|
|
37
|
+
text: string;
|
|
38
|
+
segmentId: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface DictationRuntimeOptions {
|
|
42
|
+
registry: DictationTargetRegistry;
|
|
43
|
+
getStatus(): DictationRuntimeStatus;
|
|
44
|
+
setPartial(partial: DictationPartialState | null): void;
|
|
45
|
+
setError?(error: { code: string; message: string } | null): void;
|
|
46
|
+
setStatus?(status: DictationRuntimeStatus): void;
|
|
47
|
+
resetSessionContext(options?: { waitForAck?: boolean }): Promise<void>;
|
|
48
|
+
flashRange?(
|
|
49
|
+
binding: TargetBinding,
|
|
50
|
+
start: number,
|
|
51
|
+
end: number,
|
|
52
|
+
variant?: "revised" | "committed"
|
|
53
|
+
): void;
|
|
54
|
+
voiceFormatting?: VoiceFormattingConfig;
|
|
55
|
+
getVoiceFormatting?: () => VoiceFormattingConfig | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isEphiaControlEventTarget(target: EventTarget | null): boolean {
|
|
59
|
+
if (!(target instanceof Element)) return false;
|
|
60
|
+
return Boolean(
|
|
61
|
+
target.closest(
|
|
62
|
+
'[data-ephia-control="true"], [data-ephia-status], .ephia-transcribe-root, .ephia-transcribe-floating-root'
|
|
63
|
+
)
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isModifierOnlyKey(event: Event): boolean {
|
|
68
|
+
if (!(event instanceof KeyboardEvent)) return false;
|
|
69
|
+
return ["Alt", "Control", "Meta", "Shift", "CapsLock"].includes(event.key);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function asArray(value: string | string[] | undefined): string[] {
|
|
73
|
+
if (!value) return [];
|
|
74
|
+
return Array.isArray(value) ? value : [value];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Délai minimal entre realtime_final et son commit définitif — laisse le temps
|
|
78
|
+
// à la correction Small d'arriver avant que le realtime ne soit visible.
|
|
79
|
+
// Réduit à 150 ms : Small reçoit l'audio seul, sa latence est plus prévisible.
|
|
80
|
+
const REALTIME_COMMIT_HOLD_MS = 150;
|
|
81
|
+
|
|
82
|
+
export class DictationRuntime {
|
|
83
|
+
private readonly registry: DictationTargetRegistry;
|
|
84
|
+
private readonly opts: DictationRuntimeOptions;
|
|
85
|
+
private readonly segmentTargetMap = new Map<string, string>();
|
|
86
|
+
private readonly pendingRealtimeCommits = new Map<string, ReturnType<typeof setTimeout>>();
|
|
87
|
+
private readonly absorbedSegmentIds = new Set<string>();
|
|
88
|
+
private activePartial: DictationPartialState | null = null;
|
|
89
|
+
private interruptResetInFlight = false;
|
|
90
|
+
private sessionClosed = false;
|
|
91
|
+
|
|
92
|
+
constructor(opts: DictationRuntimeOptions) {
|
|
93
|
+
this.opts = opts;
|
|
94
|
+
this.registry = opts.registry;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
handleEvent(event: EphiaAudioEvent): void {
|
|
98
|
+
if (
|
|
99
|
+
this.sessionClosed &&
|
|
100
|
+
(event.type.startsWith("transcript.") || event.type.startsWith("segment."))
|
|
101
|
+
) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
switch (event.type) {
|
|
106
|
+
case "session.ready":
|
|
107
|
+
this.sessionClosed = false;
|
|
108
|
+
break;
|
|
109
|
+
|
|
110
|
+
case "transcript.preview":
|
|
111
|
+
case "transcript.preview.stable":
|
|
112
|
+
this.handlePreview(event);
|
|
113
|
+
break;
|
|
114
|
+
|
|
115
|
+
case "transcript.segment.ready":
|
|
116
|
+
this.handleSegmentReady(event);
|
|
117
|
+
break;
|
|
118
|
+
|
|
119
|
+
case "transcript.final":
|
|
120
|
+
case "transcript.segment.committed":
|
|
121
|
+
this.handleCommitted(event);
|
|
122
|
+
break;
|
|
123
|
+
|
|
124
|
+
case "segment.absorbed":
|
|
125
|
+
this.segmentTargetMap.delete(event.payload.segmentId);
|
|
126
|
+
break;
|
|
127
|
+
|
|
128
|
+
case "segment.dropped":
|
|
129
|
+
this.handleDropped(event.payload.segmentId);
|
|
130
|
+
break;
|
|
131
|
+
|
|
132
|
+
case "transcript.segment.error":
|
|
133
|
+
this.clearSegmentState(event.payload.segmentId, { clearPartial: true });
|
|
134
|
+
break;
|
|
135
|
+
|
|
136
|
+
case "session.closed":
|
|
137
|
+
this.sessionClosed = true;
|
|
138
|
+
this.setPartial(null);
|
|
139
|
+
this.clearMappedTargetStates({ clearBindings: true });
|
|
140
|
+
this.segmentTargetMap.clear();
|
|
141
|
+
this.absorbedSegmentIds.clear();
|
|
142
|
+
this.clearPendingRealtimeCommits();
|
|
143
|
+
break;
|
|
144
|
+
|
|
145
|
+
case "session.context.reset": {
|
|
146
|
+
if (this.activePartial) {
|
|
147
|
+
const t = this.getSegmentTarget(this.activePartial.segmentId);
|
|
148
|
+
t?.binding?.clearPartial(this.activePartial.segmentId);
|
|
149
|
+
}
|
|
150
|
+
this.setPartial(null);
|
|
151
|
+
this.clearMappedTargetStates({ clearBindings: false });
|
|
152
|
+
this.segmentTargetMap.clear();
|
|
153
|
+
this.absorbedSegmentIds.clear();
|
|
154
|
+
this.clearPendingRealtimeCommits();
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
case "session.error":
|
|
159
|
+
this.sessionClosed = true;
|
|
160
|
+
this.setPartial(null);
|
|
161
|
+
this.clearMappedTargetStates({ clearBindings: true });
|
|
162
|
+
this.opts.setError?.(event.payload);
|
|
163
|
+
this.opts.setStatus?.("error");
|
|
164
|
+
this.segmentTargetMap.clear();
|
|
165
|
+
this.absorbedSegmentIds.clear();
|
|
166
|
+
this.clearPendingRealtimeCommits();
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private clearPendingRealtimeCommits(): void {
|
|
172
|
+
for (const timer of this.pendingRealtimeCommits.values()) clearTimeout(timer);
|
|
173
|
+
this.pendingRealtimeCommits.clear();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
handleFallbackFinal(text: string, segmentId: string): boolean {
|
|
177
|
+
const targetId = this.segmentTargetMap.get(segmentId) ?? this.registry.getActiveTargetId();
|
|
178
|
+
if (!targetId) return false;
|
|
179
|
+
|
|
180
|
+
const target = this.registry.get(targetId);
|
|
181
|
+
if (target?.insertion !== "none" && target?.binding) {
|
|
182
|
+
target.binding.commitFinal(segmentId, this.formatCommittedText(text));
|
|
183
|
+
}
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
handleUserInteraction(event: Event): void {
|
|
188
|
+
const status = this.opts.getStatus();
|
|
189
|
+
const partial = this.activePartial;
|
|
190
|
+
if (!partial) return;
|
|
191
|
+
if (status === "idle" || status === "error") return;
|
|
192
|
+
if (isModifierOnlyKey(event) || isEphiaControlEventTarget(event.target)) return;
|
|
193
|
+
|
|
194
|
+
const activeTargetId = this.registry.getActiveTargetId();
|
|
195
|
+
if (!activeTargetId || (partial.targetId && partial.targetId !== activeTargetId)) return;
|
|
196
|
+
|
|
197
|
+
const target = this.registry.get(activeTargetId);
|
|
198
|
+
const eventTarget = event.target;
|
|
199
|
+
if (!target?.binding || !(eventTarget instanceof Node)) return;
|
|
200
|
+
if (!target.element.contains(eventTarget)) return;
|
|
201
|
+
|
|
202
|
+
this.setPartial(null);
|
|
203
|
+
target.binding.clearAll?.();
|
|
204
|
+
this.clearTargetState(target);
|
|
205
|
+
|
|
206
|
+
if (!this.interruptResetInFlight) {
|
|
207
|
+
this.interruptResetInFlight = true;
|
|
208
|
+
this.opts.resetSessionContext({ waitForAck: false }).finally(() => {
|
|
209
|
+
this.interruptResetInFlight = false;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
clear(): void {
|
|
215
|
+
this.segmentTargetMap.clear();
|
|
216
|
+
this.absorbedSegmentIds.clear();
|
|
217
|
+
this.clearPendingRealtimeCommits();
|
|
218
|
+
this.interruptResetInFlight = false;
|
|
219
|
+
this.sessionClosed = false;
|
|
220
|
+
this.setPartial(null);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private getVoiceFormatting(): VoiceFormattingConfig | undefined {
|
|
224
|
+
return this.opts.getVoiceFormatting?.() ?? this.opts.voiceFormatting;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private formatPreviewText(text: string): string {
|
|
228
|
+
return normalizePreviewText(text, this.getVoiceFormatting());
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private formatCommittedText(text: string): string {
|
|
232
|
+
return normalizeCommittedText(text, this.getVoiceFormatting());
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private handlePreview(
|
|
236
|
+
event: Extract<EphiaAudioEvent, { type: "transcript.preview" | "transcript.preview.stable" }>
|
|
237
|
+
): void {
|
|
238
|
+
const { segmentId, revision } = event.payload;
|
|
239
|
+
const text = this.formatPreviewText(event.payload.text);
|
|
240
|
+
const targetId = this.resolveSegmentTargetId(segmentId);
|
|
241
|
+
|
|
242
|
+
this.setPartial({ targetId, text, segmentId });
|
|
243
|
+
|
|
244
|
+
const target = targetId ? this.registry.get(targetId) : undefined;
|
|
245
|
+
const insertion = target?.insertion ?? "preview-inline";
|
|
246
|
+
if (insertion === "preview-inline" || insertion === "preview-floating") {
|
|
247
|
+
target?.binding?.insertPartial?.(segmentId, text, revision ?? 0);
|
|
248
|
+
}
|
|
249
|
+
target?.element?.setAttribute("data-ephia-target-processing", "true");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private handleSegmentReady(
|
|
253
|
+
event: Extract<EphiaAudioEvent, { type: "transcript.segment.ready" }>
|
|
254
|
+
): void {
|
|
255
|
+
const { segmentId, text, delta } = event.payload;
|
|
256
|
+
const effectiveText = this.formatCommittedText(pickEffectiveText({ text, delta }));
|
|
257
|
+
const targetId = this.resolveSegmentTargetId(segmentId);
|
|
258
|
+
const target = targetId ? this.registry.get(targetId) : undefined;
|
|
259
|
+
const insertion = target?.insertion ?? "preview-inline";
|
|
260
|
+
|
|
261
|
+
if (target?.binding?.insertPartial && insertion !== "none") {
|
|
262
|
+
target.binding.insertPartial(segmentId, effectiveText, Number.MAX_SAFE_INTEGER);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private handleCommitted(
|
|
267
|
+
event: Extract<EphiaAudioEvent, { type: "transcript.final" | "transcript.segment.committed" }>
|
|
268
|
+
): void {
|
|
269
|
+
const { segmentId } = event.payload as { segmentId: string };
|
|
270
|
+
const absorbed = asArray(
|
|
271
|
+
event.type === "transcript.segment.committed"
|
|
272
|
+
? (event.payload as { mergedWith?: string | string[] }).mergedWith
|
|
273
|
+
: undefined
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
for (const absorbedId of absorbed) {
|
|
277
|
+
this.absorbedSegmentIds.add(absorbedId);
|
|
278
|
+
const pending = this.pendingRealtimeCommits.get(absorbedId);
|
|
279
|
+
if (pending) {
|
|
280
|
+
clearTimeout(pending);
|
|
281
|
+
this.pendingRealtimeCommits.delete(absorbedId);
|
|
282
|
+
}
|
|
283
|
+
if (this.activePartial?.segmentId === absorbedId) {
|
|
284
|
+
this.setPartial(null);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (this.absorbedSegmentIds.has(segmentId)) {
|
|
289
|
+
const pending = this.pendingRealtimeCommits.get(segmentId);
|
|
290
|
+
if (pending) {
|
|
291
|
+
clearTimeout(pending);
|
|
292
|
+
this.pendingRealtimeCommits.delete(segmentId);
|
|
293
|
+
}
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Un realtime_final (correctionPending=true) n'est pas encore sûr : Small
|
|
298
|
+
// peut le corriger ~500-800ms plus tard. On retarde son commit pour éviter
|
|
299
|
+
// qu'un mauvais début de segment ne s'affiche brièvement comme définitif.
|
|
300
|
+
const keepProcessing =
|
|
301
|
+
event.type === "transcript.segment.committed" &&
|
|
302
|
+
(event.payload as { correctionPending?: boolean }).correctionPending === true;
|
|
303
|
+
|
|
304
|
+
if (keepProcessing) {
|
|
305
|
+
const existing = this.pendingRealtimeCommits.get(segmentId);
|
|
306
|
+
if (existing) clearTimeout(existing);
|
|
307
|
+
const timer = setTimeout(() => {
|
|
308
|
+
this.pendingRealtimeCommits.delete(segmentId);
|
|
309
|
+
this.commitEventNow(event);
|
|
310
|
+
}, REALTIME_COMMIT_HOLD_MS);
|
|
311
|
+
this.pendingRealtimeCommits.set(segmentId, timer);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const pending = this.pendingRealtimeCommits.get(segmentId);
|
|
316
|
+
if (pending) {
|
|
317
|
+
clearTimeout(pending);
|
|
318
|
+
this.pendingRealtimeCommits.delete(segmentId);
|
|
319
|
+
}
|
|
320
|
+
this.commitEventNow(event);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private commitEventNow(
|
|
324
|
+
event: Extract<EphiaAudioEvent, { type: "transcript.final" | "transcript.segment.committed" }>
|
|
325
|
+
): void {
|
|
326
|
+
const { segmentId, text, delta, textForInsertion, targetId: payloadTargetId } = event.payload as {
|
|
327
|
+
segmentId: string;
|
|
328
|
+
text: string;
|
|
329
|
+
delta?: string;
|
|
330
|
+
textForInsertion?: string;
|
|
331
|
+
targetId?: string;
|
|
332
|
+
};
|
|
333
|
+
if (this.absorbedSegmentIds.has(segmentId)) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const absorbed = asArray(
|
|
337
|
+
event.type === "transcript.segment.committed"
|
|
338
|
+
? (event.payload as { mergedWith?: string | string[] }).mergedWith
|
|
339
|
+
: undefined
|
|
340
|
+
);
|
|
341
|
+
const rawEffectiveText =
|
|
342
|
+
event.type === "transcript.segment.committed"
|
|
343
|
+
? textForInsertion ?? pickEffectiveText({ text, delta })
|
|
344
|
+
: text;
|
|
345
|
+
const effectiveText = this.formatCommittedText(rawEffectiveText);
|
|
346
|
+
const targetId = this.resolveSegmentTargetId(segmentId, payloadTargetId);
|
|
347
|
+
const target = targetId ? this.registry.get(targetId) : undefined;
|
|
348
|
+
this.setPartial(null);
|
|
349
|
+
this.clearTargetState(target);
|
|
350
|
+
|
|
351
|
+
if (target?.insertion !== "none" && target?.binding) {
|
|
352
|
+
target.binding.commitFinal(
|
|
353
|
+
segmentId,
|
|
354
|
+
effectiveText,
|
|
355
|
+
absorbed.length ? { absorbedSegmentIds: absorbed } : undefined
|
|
356
|
+
);
|
|
357
|
+
for (const absorbedId of absorbed) this.segmentTargetMap.delete(absorbedId);
|
|
358
|
+
const range = target.binding.getSegmentRange?.(segmentId);
|
|
359
|
+
if (range) this.opts.flashRange?.(target.binding, range.start, range.end, "committed");
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private handleDropped(segmentId: string): void {
|
|
364
|
+
const pending = this.pendingRealtimeCommits.get(segmentId);
|
|
365
|
+
if (pending) {
|
|
366
|
+
clearTimeout(pending);
|
|
367
|
+
this.pendingRealtimeCommits.delete(segmentId);
|
|
368
|
+
}
|
|
369
|
+
this.setPartial(null);
|
|
370
|
+
const target = this.getSegmentTarget(segmentId);
|
|
371
|
+
target?.binding?.clearPartial(segmentId);
|
|
372
|
+
this.clearTargetState(target);
|
|
373
|
+
this.segmentTargetMap.delete(segmentId);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private clearSegmentState(
|
|
377
|
+
segmentId: string,
|
|
378
|
+
opts: { clearPartial: boolean }
|
|
379
|
+
): void {
|
|
380
|
+
if (opts.clearPartial) this.setPartial(null);
|
|
381
|
+
const target = this.getSegmentTarget(segmentId);
|
|
382
|
+
if (opts.clearPartial) target?.binding?.clearPartial(segmentId);
|
|
383
|
+
this.clearTargetState(target);
|
|
384
|
+
this.segmentTargetMap.delete(segmentId);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
private clearTargetState(target: DictationTarget | undefined): void {
|
|
388
|
+
target?.element?.removeAttribute("data-ephia-target-processing");
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private clearMappedTargetStates(opts: { clearBindings: boolean }): void {
|
|
392
|
+
const targetIds = new Set(this.segmentTargetMap.values());
|
|
393
|
+
const activeTargetId = this.registry.getActiveTargetId();
|
|
394
|
+
if (activeTargetId) targetIds.add(activeTargetId);
|
|
395
|
+
|
|
396
|
+
for (const targetId of targetIds) {
|
|
397
|
+
const target = this.registry.get(targetId);
|
|
398
|
+
if (opts.clearBindings) target?.binding?.clearAll?.();
|
|
399
|
+
this.clearTargetState(target);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private setPartial(partial: DictationPartialState | null): void {
|
|
404
|
+
this.activePartial = partial;
|
|
405
|
+
this.opts.setPartial(partial);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private resolveSegmentTargetId(
|
|
409
|
+
segmentId: string | undefined,
|
|
410
|
+
payloadTargetId?: string
|
|
411
|
+
): string | null {
|
|
412
|
+
const activeTargetId = this.registry.getActiveTargetId();
|
|
413
|
+
if (!segmentId) {
|
|
414
|
+
if (payloadTargetId && this.registry.get(payloadTargetId)) return payloadTargetId;
|
|
415
|
+
return payloadTargetId ? null : activeTargetId;
|
|
416
|
+
}
|
|
417
|
+
const mappedTargetId = this.segmentTargetMap.get(segmentId);
|
|
418
|
+
if (mappedTargetId) return mappedTargetId;
|
|
419
|
+
if (payloadTargetId) {
|
|
420
|
+
if (!this.registry.get(payloadTargetId)) return null;
|
|
421
|
+
this.segmentTargetMap.set(segmentId, payloadTargetId);
|
|
422
|
+
return payloadTargetId;
|
|
423
|
+
}
|
|
424
|
+
if (!this.segmentTargetMap.has(segmentId) && activeTargetId) {
|
|
425
|
+
this.segmentTargetMap.set(segmentId, activeTargetId);
|
|
426
|
+
}
|
|
427
|
+
return this.segmentTargetMap.get(segmentId) ?? activeTargetId;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private getSegmentTarget(segmentId: string): DictationTarget | undefined {
|
|
431
|
+
const targetId = this.resolveSegmentTargetId(segmentId);
|
|
432
|
+
return targetId ? this.registry.get(targetId) : undefined;
|
|
433
|
+
}
|
|
434
|
+
}
|