File size: 4,696 Bytes
17c377a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { randomUUID } from "crypto";
import type {
  TranscriptionJob,
  SrtSegment,
  Anomaly,
  JobStatus,
  CorrectionRequest
} from "@shared/schema";

export interface IStorage {
  createJob(fileName: string, fileSize: number, fileType: string, userContext?: string): Promise<TranscriptionJob>;
  getJob(id: string): Promise<TranscriptionJob | undefined>;
  updateJobStatus(id: string, status: JobStatus, progress?: number): Promise<void>;
  updateJobError(id: string, errorMessage: string): Promise<void>;
  setJobSegments(id: string, segments: SrtSegment[]): Promise<void>;
  setJobAnomalies(id: string, anomalies: Anomaly[]): Promise<void>;
  applyCorrection(jobId: string, anomalyId: string, correction: string): Promise<void>;
  applySimilarCorrections(jobId: string, flaggedText: string, correction: string): Promise<number>;
  updateSegmentText(jobId: string, segmentId: number, newText: string): Promise<void>;
}

export class MemStorage implements IStorage {
  private jobs: Map<string, TranscriptionJob>;

  constructor() {
    this.jobs = new Map();
  }

  async createJob(
    fileName: string,
    fileSize: number,
    fileType: string,
    userContext?: string,
    localFilePath?: string
  ): Promise<TranscriptionJob> {
    const id = randomUUID();
    const job: TranscriptionJob = {
      id,
      fileName,
      fileSize,
      fileType: (fileType || "audio/mpeg") as "mp3" | "mp4" | "audio/mpeg" | "audio/mp3" | "video/mp4",
      status: "pending",
      progress: 0,
      userContext,
      mediaUrl: `/api/jobs/${id}/media`,
      createdAt: new Date().toISOString(),
    };

    // Store local path internally (not in schema)
    (job as any).localFilePath = localFilePath;

    this.jobs.set(id, job);
    return job;
  }

  async getJob(id: string): Promise<TranscriptionJob | undefined> {
    return this.jobs.get(id);
  }

  async updateJobStatus(id: string, status: JobStatus, progress?: number): Promise<void> {
    const job = this.jobs.get(id);
    if (job) {
      job.status = status;
      if (progress !== undefined) {
        job.progress = progress;
      }
      if (status === "completed") {
        job.completedAt = new Date().toISOString();
      }
      this.jobs.set(id, job);
    }
  }

  async updateJobError(id: string, errorMessage: string): Promise<void> {
    const job = this.jobs.get(id);
    if (job) {
      job.status = "failed";
      job.errorMessage = errorMessage;
      this.jobs.set(id, job);
    }
  }

  async setJobSegments(id: string, segments: SrtSegment[]): Promise<void> {
    const job = this.jobs.get(id);
    if (job) {
      job.segments = segments;
      this.jobs.set(id, job);
    }
  }

  async setJobAnomalies(id: string, anomalies: Anomaly[]): Promise<void> {
    const job = this.jobs.get(id);
    if (job) {
      job.anomalies = anomalies;
      this.jobs.set(id, job);
    }
  }

  async applyCorrection(jobId: string, anomalyId: string, correction: string): Promise<void> {
    const job = this.jobs.get(jobId);
    if (!job || !job.anomalies || !job.segments) return;

    const anomaly = job.anomalies.find(a => a.id === anomalyId);
    if (!anomaly) return;

    // Update the anomaly
    anomaly.resolved = true;
    anomaly.userCorrection = correction;

    // Update the segment text
    const segment = job.segments.find(s => s.id === anomaly.segmentId);
    if (segment) {
      segment.text = segment.text.replace(anomaly.flaggedText, correction);
    }

    this.jobs.set(jobId, job);
  }

  async applySimilarCorrections(jobId: string, flaggedText: string, correction: string): Promise<number> {
    const job = this.jobs.get(jobId);
    if (!job || !job.anomalies || !job.segments) return 0;

    let correctionCount = 0;
    const lowerFlaggedText = flaggedText.toLowerCase();

    for (const anomaly of job.anomalies) {
      if (!anomaly.resolved && anomaly.flaggedText.toLowerCase() === lowerFlaggedText) {
        anomaly.resolved = true;
        anomaly.userCorrection = correction;
        correctionCount++;

        // Update the segment text
        const segment = job.segments.find(s => s.id === anomaly.segmentId);
        if (segment) {
          segment.text = segment.text.replace(anomaly.flaggedText, correction);
        }
      }
    }

    this.jobs.set(jobId, job);
    return correctionCount;
  }

  async updateSegmentText(jobId: string, segmentId: number, newText: string): Promise<void> {
    const job = this.jobs.get(jobId);
    if (!job || !job.segments) return;

    const segment = job.segments.find(s => s.id === segmentId);
    if (segment) {
      segment.text = newText;
      this.jobs.set(jobId, job);
    }
  }
}

export const storage = new MemStorage();