( videoId: Video.VideoId, userId: string, )
| 13 | }; |
| 14 | |
| 15 | export async function startAiGeneration( |
| 16 | videoId: Video.VideoId, |
| 17 | userId: string, |
| 18 | ): Promise<GenerateAiResult> { |
| 19 | if (!serverEnv().GROQ_API_KEY && !serverEnv().OPENAI_API_KEY) { |
| 20 | return { |
| 21 | success: false, |
| 22 | message: "Missing AI API keys (Groq or OpenAI)", |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | if (!userId || !videoId) { |
| 27 | return { |
| 28 | success: false, |
| 29 | message: "userId or videoId not supplied", |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | const query = await db() |
| 34 | .select({ video: videos }) |
| 35 | .from(videos) |
| 36 | .where(eq(videos.id, videoId)); |
| 37 | |
| 38 | if (query.length === 0 || !query[0]?.video) { |
| 39 | return { success: false, message: "Video does not exist" }; |
| 40 | } |
| 41 | |
| 42 | const { video } = query[0]; |
| 43 | |
| 44 | if (video.transcriptionStatus !== "COMPLETE") { |
| 45 | return { |
| 46 | success: false, |
| 47 | message: "Transcription not complete", |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | const metadata = (video.metadata as VideoMetadata) || {}; |
| 52 | |
| 53 | if ( |
| 54 | metadata.aiGenerationStatus === "PROCESSING" || |
| 55 | metadata.aiGenerationStatus === "QUEUED" |
| 56 | ) { |
| 57 | return { |
| 58 | success: true, |
| 59 | message: "AI generation already in progress", |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | if ( |
| 64 | metadata.aiGenerationStatus === "COMPLETE" && |
| 65 | metadata.summary && |
| 66 | metadata.chapters |
| 67 | ) { |
| 68 | return { |
| 69 | success: true, |
| 70 | message: "AI metadata already generated", |
| 71 | }; |
| 72 | } |
no test coverage detected