Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Trigger AI generation after transcription completion

Richie McIlroy7ccc41915214
diff --git a/apps/web/actions/videos/get-status.ts b/apps/web/actions/videos/get-status.ts
index 16edcc978..5e9f7681b 100644
--- a/apps/web/actions/videos/get-status.ts
+++ b/apps/web/actions/videos/get-status.ts
@@ -1,15 +1,17 @@
"use server";
import { db } from "@cap/database";
-import { videos } from "@cap/database/schema";
+import { users, videos } from "@cap/database/schema";
import type { VideoMetadata } from "@cap/database/types";
import { serverEnv } from "@cap/env";
import { provideOptionalAuth, VideosPolicy } from "@cap/web-backend";
import { Policy, type Video } from "@cap/web-domain";
import { eq } from "drizzle-orm";
import { Effect, Exit } from "effect";
+import { startAiGeneration } from "@/lib/generate-ai";
import * as EffectRuntime from "@/lib/server";
import { transcribeVideo } from "../../lib/transcribe";
+import { isAiGenerationEnabled } from "../../utils/flags";
type TranscriptionStatus =
| "PROCESSING"
@@ -103,6 +105,53 @@ export async function getVideoStatus(
};
}
+ const shouldTriggerAiGeneration =
+ video.transcriptionStatus === "COMPLETE" &&
+ !metadata.aiGenerationStatus &&
+ !metadata.summary &&
+ (serverEnv().GROQ_API_KEY || serverEnv().OPENAI_API_KEY);
+
+ if (shouldTriggerAiGeneration) {
+ try {
+ const ownerQuery = await db()
+ .select({
+ email: users.email,
+ stripeSubscriptionStatus: users.stripeSubscriptionStatus,
+ thirdPartyStripeSubscriptionId: users.thirdPartyStripeSubscriptionId,
+ })
+ .from(users)
+ .where(eq(users.id, video.ownerId))
+ .limit(1);
+
+ const owner = ownerQuery[0];
+ if (owner && (await isAiGenerationEnabled(owner))) {
+ console.log(
+ `[Get Status] AI generation not started for video ${videoId}, triggering generation`,
+ );
+ startAiGeneration(videoId, video.ownerId).catch((error) => {
+ console.error(
+ `[Get Status] Error starting AI generation for video ${videoId}:`,
+ error,
+ );
+ });
+
+ return {
+ transcriptionStatus:
+ (video.transcriptionStatus as TranscriptionStatus) || null,
+ aiGenerationStatus: "QUEUED" as AiGenerationStatus,
+ aiTitle: metadata.aiTitle || null,
+ summary: metadata.summary || null,
+ chapters: metadata.chapters || null,
+ };
+ }
+ } catch (error) {
+ console.error(
+ `[Get Status] Error checking AI generation eligibility for video ${videoId}:`,
+ error,
+ );
+ }
+ }
+
return {
transcriptionStatus:
(video.transcriptionStatus as TranscriptionStatus) || null,
diff --git a/packages/database/types/metadata.ts b/packages/database/types/metadata.ts
index c4c76beda..8efb6e6fc 100644
--- a/packages/database/types/metadata.ts
+++ b/packages/database/types/metadata.ts
@@ -27,8 +27,12 @@ export interface VideoMetadata {
* Chapter markers generated from the transcript
*/
chapters?: { title: string; start: number }[];
- aiProcessing?: boolean;
- aiGenerationSkipped?: boolean;
+ aiGenerationStatus?:
+ | "QUEUED"
+ | "PROCESSING"
+ | "COMPLETE"
+ | "ERROR"
+ | "SKIPPED";
}
/**