Skip to content

Commit 19a643c

Browse files
committed
Just set up mongodb to sotre day trancriptions
1 parent f2a5d9d commit 19a643c

File tree

7 files changed

+105
-54
lines changed

7 files changed

+105
-54
lines changed

bun.lock

Lines changed: 30 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"start": "bun src/server/index.ts",
77
"dev": "concurrently \"npm run dev:backend\" \"npm run dev:frontend\"",
8+
"dev:d": "doppler run -- bun run dev",
89
"dev:backend": "bun --hot src/server/index.ts",
910
"dev:frontend": "cd src/frontend && npm run dev",
1011
"build:frontend": "cd src/frontend && npm run build",
@@ -17,7 +18,7 @@
1718
"description": "Example app for taking photos with MentraOS",
1819
"dependencies": {
1920
"@mentra/react": "^2.0.2",
20-
"@mentra/sdk": "^3.0.0-hono.2",
21+
"@mentra/sdk": "^2.1.29",
2122
"ejs": "^3.1.10",
2223
"express": "^4.21.2",
2324
"http-proxy-middleware": "^3.0.5",

src/server/modules/transcription.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/shared/class/DisplayManger.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class DisplayManager {
2+
3+
4+
private test : string;
5+
6+
constructor () {
7+
this.test = "test";
8+
}
9+
}

src/shared/class/Transcript.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TranscriptionData } from "@mentra/sdk";
2+
import { Transcript as TranscriptModel } from "../schema/transcript.schema";
23

34
export interface TranscriptSegment {
45
speakerId: string;
@@ -11,8 +12,16 @@ export interface TranscriptSegment {
1112

1213
export 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
}

src/shared/class/User.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import { AppSession, TranscriptionData } from "@mentra/sdk";
1+
import { AppSession, TranscriptionData, } from "@mentra/sdk";
22
import { Transcript } from "./Transcript";
33
import { sendTranscription } from "../../server/stream/sse";
4-
4+
import { DisplayManager } from "./DisplayManger";
55
export class User {
66
private static readonly sessions: Map<string, User> = new Map();
77

88
readonly userId: string;
99
readonly session: AppSession;
1010
readonly transcript: Transcript;
11-
11+
readonly display: DisplayManager;
12+
1213
private transcriptionCleanup: (() => void) | null = null;
1314

1415
constructor(session: AppSession) {
1516
this.userId = session.userId;
1617
this.session = session;
17-
this.transcript = new Transcript();
18+
this.transcript = new Transcript(session.userId, session.userId);
19+
this.display = new DisplayManager();
1820

1921
User.sessions.set(this.userId, this);
2022
}
@@ -28,11 +30,11 @@ export class User {
2830
}
2931

3032
this.transcriptionCleanup = this.session.events.onTranscription(
31-
(data: TranscriptionData) => {
33+
async (data: TranscriptionData) => {
3234
const speaker = data.speakerId ?? "unknown";
3335
console.log(` ${this.userId}, [${speaker}]: ${data.text} (final: ${data.isFinal})`);
3436

35-
this.transcript.addSegment(data);
37+
await this.transcript.addSegment(data);
3638
sendTranscription(this.userId, data);
3739
}
3840
);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Schema, model } from "mongoose";
2+
import { getFormatedDate } from "./util";
3+
4+
const transcriptChunkSchema = new Schema({
5+
userEmail: {
6+
type: String,
7+
required: true,
8+
index: true,
9+
},
10+
11+
directionizationId: {
12+
type: String,
13+
required: true,
14+
},
15+
16+
content: {
17+
type: String,
18+
required: true,
19+
},
20+
21+
createdAt: {
22+
type: Date,
23+
default: Date.now,
24+
},
25+
26+
date: {
27+
type: String,
28+
default: () => getFormatedDate(),
29+
},
30+
});
31+
32+
export const Transcript = model("Transcript", transcriptChunkSchema);

0 commit comments

Comments
 (0)