Spaces:
Running
Running
File size: 12,575 Bytes
9f29339 | 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | "use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var ContentProcessor_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentProcessor = void 0;
const bullmq_1 = require("@nestjs/bullmq");
const common_1 = require("@nestjs/common");
const bullmq_2 = require("bullmq");
const prisma_service_1 = require("../prisma/prisma.service");
const ai_service_1 = require("../ai/ai.service");
const export_renderer_service_1 = require("../common/export/export-renderer.service");
let ContentProcessor = ContentProcessor_1 = class ContentProcessor extends bullmq_1.WorkerHost {
prisma;
aiService;
exportRenderer;
logger = new common_1.Logger(ContentProcessor_1.name);
constructor(prisma, aiService, exportRenderer) {
super();
this.prisma = prisma;
this.aiService = aiService;
this.exportRenderer = exportRenderer;
}
async process(job) {
this.logger.log(`Processing content job ${job.id} of type ${job.name}`);
switch (job.name) {
case "transcribe":
return this.handleTranscription(job.data, job);
case "generate-clips":
return this.handleClipGeneration(job.data, job);
case "export":
return this.handleExport(job.data, job);
default:
this.logger.warn(`Unknown job type: ${job.name}`);
}
}
async handleTranscription(data, job) {
this.logger.log(`Starting transcription for episode ${data.episodeId}`);
await job.updateProgress(10);
const episode = await this.prisma.episode.findUnique({
where: { id: data.episodeId },
include: {
podcast: {
select: { workspaceId: true }
}
}
});
if (!episode) {
throw new Error(`Episode with ID ${data.episodeId} not found`);
}
const workspaceId = episode.podcast.workspaceId;
await job.updateProgress(30);
let audioBuffer;
try {
if (data.audioUrl && data.audioUrl.startsWith("http")) {
const audioResponse = await fetch(data.audioUrl);
if (!audioResponse.ok) {
throw new Error(`Failed to download audio: ${audioResponse.statusText}`);
}
const arrayBuffer = await audioResponse.arrayBuffer();
audioBuffer = Buffer.from(arrayBuffer);
}
else {
audioBuffer = Buffer.from("placeholder audio data");
}
}
catch (downloadError) {
this.logger.warn(`Could not download audio from ${data.audioUrl}, using placeholder buffer. Error: ${downloadError instanceof Error ? downloadError.message : String(downloadError)}`);
audioBuffer = Buffer.from("placeholder audio data");
}
await job.updateProgress(50);
const filename = data.audioUrl ? data.audioUrl.split("/").pop() || "audio.wav" : "audio.wav";
const result = await this.aiService.transcribeAudio(workspaceId, audioBuffer, filename, {
language: data.language || "en",
});
await job.updateProgress(80);
const transcript = await this.prisma.transcript.create({
data: {
episodeId: data.episodeId,
fullText: result.text,
segments: (result.segments || []),
speakerDiarization: {},
language: result.language || data.language || "en",
confidenceScore: null,
processingDuration: null,
},
});
await job.updateProgress(100);
this.logger.log(`Transcription completed and saved for episode ${data.episodeId}`);
return {
transcriptId: transcript.id,
text: result.text,
duration: result.duration || 3600,
};
}
async handleClipGeneration(data, job) {
this.logger.log(`Generating clips for episode ${data.episodeId}`);
const candidates = await this.prisma.clip.findMany({
where: { episodeId: data.episodeId },
select: { id: true, startSeconds: true, endSeconds: true },
});
const clipIds = [];
for (let i = 0; i < data.clips.length; i++) {
const want = data.clips[i];
const progress = Math.round(((i + 1) / data.clips.length) * 100);
await job.updateProgress(progress);
const matched = candidates.find((c) => Math.abs(c.startSeconds - want.startSeconds) < 0.5 &&
Math.abs(c.endSeconds - want.endSeconds) < 0.5);
if (matched) {
clipIds.push(matched.id);
}
else {
this.logger.warn(`No pre-existing Clip row matched AI-detected range ` +
`${want.startSeconds}s-${want.endSeconds}s for episode ${data.episodeId}`);
}
}
if (clipIds.length === 0) {
this.logger.warn(`Clip generation produced no linked rows for episode ${data.episodeId}. ` +
`The AI may have detected ranges that did not match the persisted clips.`);
}
return { clipIds };
}
async handleExport(data, job) {
this.logger.log(`Exporting clip ${data.clipId} for ${data.platform}`);
await job.updateProgress(5);
const exportJob = data.exportJobId
? await this.prisma.exportJob.findUnique({
where: { id: data.exportJobId },
select: { id: true, status: true },
})
: await this.prisma.exportJob.findFirst({
where: {
clipId: data.clipId,
platform: data.platform,
},
select: { id: true, status: true },
orderBy: { createdAt: "desc" },
});
if (!exportJob) {
throw new Error(`ExportJob not found for clip=${data.clipId} platform=${data.platform}`);
}
await this.prisma.exportJob.update({
where: { id: exportJob.id },
data: { status: "rendering", startedAt: new Date() },
});
const clip = await this.prisma.clip.findUnique({
where: { id: data.clipId },
select: {
id: true,
episodeId: true,
startSeconds: true,
endSeconds: true,
suggestedHook: true,
suggestedCaption: true,
episode: {
select: {
recordingSession: {
select: {
tracks: {
select: { trackUrl: true },
where: { status: "completed" },
orderBy: { createdAt: "desc" },
take: 1,
},
},
},
podcast: {
select: { workspaceId: true },
},
},
},
},
});
if (!clip) {
throw new Error(`Clip ${data.clipId} not found for export`);
}
const workspaceId = data.workspaceId || clip.episode?.podcast?.workspaceId;
if (!workspaceId) {
throw new Error(`Cannot resolve workspaceId for export job ${exportJob.id} ` +
`(clip=${data.clipId}, platform=${data.platform})`);
}
const trackUrl = clip.episode?.recordingSession?.tracks?.[0]?.trackUrl ?? null;
const sourceUrl = data.sourceUrl ?? trackUrl ?? null;
if (!sourceUrl) {
this.logger.error(`Clip ${data.clipId} has no sourceUrl and no recording track URL. Export cannot proceed.`);
await this.prisma.exportJob.update({
where: { id: exportJob.id },
data: {
status: "failed",
errorMessage: "No source audio URL available. Ensure the recording is completed before exporting.",
},
});
throw new Error(`Clip ${data.clipId} has no source media URL. ` +
`Complete the recording and processing pipeline before exporting.`);
}
await job.updateProgress(10);
const caption = data.caption || clip.suggestedCaption || undefined;
const hook = data.hook || clip.suggestedHook || undefined;
try {
const result = await this.exportRenderer.renderAndUpload({
exportJobId: exportJob.id,
clipId: data.clipId,
platform: data.platform,
sourceUrl,
caption,
hook,
renderingParams: data.renderingParams || {},
workspaceId,
episodeId: data.episodeId || clip.episodeId,
}, sourceUrl, clip.startSeconds, clip.endSeconds, (progress) => job.updateProgress(10 + Math.round(progress * 0.85)));
if (!result) {
throw new Error(`Export rendering returned null for clip ${data.clipId}`);
}
await this.prisma.exportJob.update({
where: { id: exportJob.id },
data: {
outputUrl: result.cdnUrl,
status: "completed",
completedAt: new Date(),
},
});
await job.updateProgress(100);
this.logger.log(`Export completed for clip ${data.clipId} (${data.platform}): ${result.cdnUrl} (${result.fileSizeBytes} bytes)`);
return { url: result.cdnUrl, r2Key: result.r2Key };
}
catch (err) {
const errorMessage = err instanceof Error ? err.message : String(err);
this.logger.error(`Export failed for clip ${data.clipId} (${data.platform}): ${errorMessage}`);
await this.prisma.exportJob.update({
where: { id: exportJob.id },
data: {
status: "failed",
errorMessage,
},
});
throw err;
}
}
onCompleted(job) {
this.logger.log(`Content job ${job.id} completed successfully`);
}
onFailed(job, error) {
this.logger.error(`Content job ${job.id} failed: ${error.message}`, error.stack);
}
onProgress(job, progress) {
this.logger.debug(`Content job ${job.id} progress: ${progress}%`);
}
};
exports.ContentProcessor = ContentProcessor;
__decorate([
(0, bullmq_1.OnWorkerEvent)("completed"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [bullmq_2.Job]),
__metadata("design:returntype", void 0)
], ContentProcessor.prototype, "onCompleted", null);
__decorate([
(0, bullmq_1.OnWorkerEvent)("failed"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [bullmq_2.Job, Error]),
__metadata("design:returntype", void 0)
], ContentProcessor.prototype, "onFailed", null);
__decorate([
(0, bullmq_1.OnWorkerEvent)("progress"),
__metadata("design:type", Function),
__metadata("design:paramtypes", [bullmq_2.Job, Number]),
__metadata("design:returntype", void 0)
], ContentProcessor.prototype, "onProgress", null);
exports.ContentProcessor = ContentProcessor = ContentProcessor_1 = __decorate([
(0, bullmq_1.Processor)("content", {
concurrency: 2,
}),
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
ai_service_1.AIService,
export_renderer_service_1.ExportRendererService])
], ContentProcessor);
//# sourceMappingURL=content.processor.js.map |