diff --git a/.changeset/spotty-years-return.md b/.changeset/spotty-years-return.md new file mode 100644 index 0000000000..e2971f769f --- /dev/null +++ b/.changeset/spotty-years-return.md @@ -0,0 +1,5 @@ +--- +'livekit-client': patch +--- + +Add firstReceivedTime and lastReceivedTime to received TranscriptionSegments diff --git a/src/room/Room.ts b/src/room/Room.ts index 9e2e50f49c..1ef616c640 100644 --- a/src/room/Room.ts +++ b/src/room/Room.ts @@ -162,6 +162,11 @@ class Room extends (EventEmitter as new () => TypedEmitter) private isResuming: boolean = false; + /** + * map to store first point in time when a particular transcription segment was received + */ + private transcriptionReceivedTimes: Map; + /** * Creates a new Room, the primary construct for a LiveKit session. * @param options @@ -174,6 +179,7 @@ class Room extends (EventEmitter as new () => TypedEmitter) this.options = { ...roomOptionDefaults, ...options }; this.log = getLogger(this.options.loggerName ?? LoggerNames.Room); + this.transcriptionReceivedTimes = new Map(); this.options.audioCaptureDefaults = { ...audioDefaults, @@ -1274,6 +1280,7 @@ class Room extends (EventEmitter as new () => TypedEmitter) this.clearConnectionReconcile(); this.isResuming = false; this.bufferedEvents = []; + this.transcriptionReceivedTimes.clear(); if (this.state === ConnectionState.Disconnected) { return; } @@ -1535,7 +1542,7 @@ class Room extends (EventEmitter as new () => TypedEmitter) : this.getParticipantByIdentity(transcription.transcribedParticipantIdentity); const publication = participant?.trackPublications.get(transcription.trackId); - const segments = extractTranscriptionSegments(transcription); + const segments = extractTranscriptionSegments(transcription, this.transcriptionReceivedTimes); publication?.emit(TrackEvent.TranscriptionReceived, segments); participant?.emit(ParticipantEvent.TranscriptionReceived, segments, publication); diff --git a/src/room/types.ts b/src/room/types.ts index e3f12a8417..a92127f478 100644 --- a/src/room/types.ts +++ b/src/room/types.ts @@ -65,4 +65,6 @@ export interface TranscriptionSegment { startTime: number; endTime: number; final: boolean; + firstReceivedTime: number; + lastReceivedTime: number; } diff --git a/src/room/utils.ts b/src/room/utils.ts index e16a6b769b..34037746f1 100644 --- a/src/room/utils.ts +++ b/src/room/utils.ts @@ -532,8 +532,16 @@ export function toHttpUrl(url: string): string { export function extractTranscriptionSegments( transcription: TranscriptionModel, + firstReceivedTimesMap: Map, ): TranscriptionSegment[] { return transcription.segments.map(({ id, text, language, startTime, endTime, final }) => { + const firstReceivedTime = firstReceivedTimesMap.get(id) ?? Date.now(); + const lastReceivedTime = Date.now(); + if (final) { + firstReceivedTimesMap.delete(id); + } else { + firstReceivedTimesMap.set(id, firstReceivedTime); + } return { id, text, @@ -541,6 +549,8 @@ export function extractTranscriptionSegments( endTime: Number.parseInt(endTime.toString()), final, language, + firstReceivedTime, + lastReceivedTime, }; }); }