11import { TranscriptionData } from "@mentra/sdk" ;
2+ import { Transcript as TranscriptModel } from "../schema/transcript.schema" ;
23
34export interface TranscriptSegment {
45 speakerId : string ;
@@ -11,8 +12,16 @@ export interface TranscriptSegment {
1112
1213export class Transcript {
1314 private segments : TranscriptSegment [ ] = [ ] ;
15+ private userEmail : string ;
16+ private directionizationId : string ;
1417
15- addSegment ( data : TranscriptionData ) : void {
18+ constructor ( userEmail : string , directionizationId : string ) {
19+ this . userEmail = userEmail ;
20+ this . directionizationId = directionizationId ;
21+ }
22+
23+ // Converts incoming transcription data into a segment and adds it to the transcript if finalized
24+ async addSegment ( data : TranscriptionData ) : Promise < void > {
1625 const segment : TranscriptSegment = {
1726 speakerId : data . speakerId ?? "unknown" ,
1827 text : data . text ,
@@ -24,17 +33,31 @@ export class Transcript {
2433
2534 if ( data . isFinal ) {
2635 this . segments . push ( segment ) ;
36+
37+ // Save to MongoDB when finalized
38+ try {
39+ await TranscriptModel . create ( {
40+ userEmail : this . userEmail ,
41+ directionizationId : segment . speakerId ,
42+ content : segment . text ,
43+ } ) ;
44+ } catch ( error ) {
45+ console . error ( "Failed to save transcript to MongoDB:" , error ) ;
46+ }
2747 }
2848 }
2949
50+ // Returns a copy of all transcript segments
3051 getSegments ( ) : TranscriptSegment [ ] {
3152 return [ ...this . segments ] ;
3253 }
3354
55+ // Combines all segment text into a single string with spaces between segments
3456 getFullText ( ) : string {
3557 return this . segments . map ( ( s ) => s . text ) . join ( " " ) ;
3658 }
3759
60+ // Clears all transcript segments
3861 clear ( ) : void {
3962 this . segments = [ ] ;
4063 }
0 commit comments