-
Notifications
You must be signed in to change notification settings - Fork 1.1k
chore: fix go to dashboard link + refactor ai generator #994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4dd1bb8
Update Share.tsx
ameer2468 4a76034
Update Share.tsx
ameer2468 c16ed6f
Update Share.tsx
ameer2468 4ffa0bc
refactor
ameer2468 15b4174
additional revalidation
ameer2468 c4fd21a
Update Share.tsx
ameer2468 07df1ad
Update Share.tsx
ameer2468 e2a120e
fix go to dashboard link and improvements to loading of generated ai …
ameer2468 beae1a7
Update get-status.ts
ameer2468 38bde92
Update get-status.ts
ameer2468 616e116
Update get-status.ts
ameer2468 8fba693
Potential fix for code scanning alert no. 49: Use of externally-contr…
ameer2468 79dd9db
Update get-status.ts
ameer2468 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,52 @@ | ||
| "use server"; | ||
|
|
||
| import { GetObjectCommand } from "@aws-sdk/client-s3"; | ||
| import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
| import { db } from "@cap/database"; | ||
| import { s3Buckets, videos } from "@cap/database/schema"; | ||
| import type { VideoMetadata } from "@cap/database/types"; | ||
| import { serverEnv } from "@cap/env"; | ||
| import { eq } from "drizzle-orm"; | ||
| import { GROQ_MODEL, getGroqClient } from "@/lib/groq-client"; | ||
| import { createBucketProvider } from "@/utils/s3"; | ||
|
|
||
| async function callOpenAI(prompt: string): Promise<string> { | ||
| const aiRes = await fetch("https://api.openai.com/v1/chat/completions", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${serverEnv().OPENAI_API_KEY}`, | ||
| }, | ||
| body: JSON.stringify({ | ||
| model: "gpt-4o-mini", | ||
| messages: [{ role: "user", content: prompt }], | ||
| }), | ||
| }); | ||
| if (!aiRes.ok) { | ||
| const errorText = await aiRes.text(); | ||
| console.error( | ||
| `[generateAiMetadata] OpenAI API error: ${aiRes.status} ${errorText}`, | ||
| ); | ||
| throw new Error(`OpenAI API error: ${aiRes.status} ${errorText}`); | ||
| } | ||
| const aiJson = await aiRes.json(); | ||
| return aiJson.choices?.[0]?.message?.content || "{}"; | ||
| } | ||
|
|
||
| async function setAiProcessingFlag( | ||
| videoId: string, | ||
| processing: boolean, | ||
| currentMetadata: VideoMetadata, | ||
| ) { | ||
| await db() | ||
| .update(videos) | ||
| .set({ | ||
| metadata: { | ||
| ...currentMetadata, | ||
| aiProcessing: processing, | ||
| }, | ||
| }) | ||
| .where(eq(videos.id, videoId)); | ||
| } | ||
|
|
||
| export async function generateAiMetadata(videoId: string, userId: string) { | ||
| const groqClient = getGroqClient(); | ||
| if (!groqClient && !serverEnv().OPENAI_API_KEY) { | ||
|
|
@@ -17,38 +55,30 @@ export async function generateAiMetadata(videoId: string, userId: string) { | |
| ); | ||
| return; | ||
| } | ||
| const videoQuery = await db() | ||
| .select({ video: videos }) | ||
|
|
||
| // Single optimized query to get video data with bucket info | ||
| const query = await db() | ||
| .select({ video: videos, bucket: s3Buckets }) | ||
| .from(videos) | ||
| .leftJoin(s3Buckets, eq(videos.bucket, s3Buckets.id)) | ||
| .where(eq(videos.id, videoId)); | ||
|
|
||
| if (videoQuery.length === 0 || !videoQuery[0]?.video) { | ||
| if (query.length === 0 || !query[0]?.video) { | ||
| console.error( | ||
| `[generateAiMetadata] Video ${videoId} not found in database`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const videoData = videoQuery[0].video; | ||
| const metadata = (videoData.metadata as VideoMetadata) || {}; | ||
| const { video: videoData, bucket: bucketData } = query[0]; | ||
| const metadata: VideoMetadata = (videoData.metadata as VideoMetadata) || {}; | ||
|
|
||
| if (metadata.aiProcessing === true) { | ||
| const updatedAtTime = new Date(videoData.updatedAt).getTime(); | ||
| const currentTime = new Date().getTime(); | ||
| const tenMinutesInMs = 10 * 60 * 1000; | ||
| const minutesElapsed = Math.round((currentTime - updatedAtTime) / 60000); | ||
|
|
||
| if (currentTime - updatedAtTime > tenMinutesInMs) { | ||
| await db() | ||
| .update(videos) | ||
| .set({ | ||
| metadata: { | ||
| ...metadata, | ||
| aiProcessing: false, | ||
| }, | ||
| }) | ||
| .where(eq(videos.id, videoId)); | ||
|
|
||
| if (Date.now() - updatedAtTime > tenMinutesInMs) { | ||
| await setAiProcessingFlag(videoId, false, metadata); | ||
| metadata.aiProcessing = false; | ||
| } else { | ||
| return; | ||
|
|
@@ -57,74 +87,31 @@ export async function generateAiMetadata(videoId: string, userId: string) { | |
|
|
||
| if (metadata.summary || metadata.chapters) { | ||
| if (metadata.aiProcessing) { | ||
| await db() | ||
| .update(videos) | ||
| .set({ | ||
| metadata: { | ||
| ...metadata, | ||
| aiProcessing: false, | ||
| }, | ||
| }) | ||
| .where(eq(videos.id, videoId)); | ||
| await setAiProcessingFlag(videoId, false, metadata); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (videoData?.transcriptionStatus !== "COMPLETE") { | ||
| if (metadata.aiProcessing) { | ||
| await db() | ||
| .update(videos) | ||
| .set({ | ||
| metadata: { | ||
| ...metadata, | ||
| aiProcessing: false, | ||
| }, | ||
| }) | ||
| .where(eq(videos.id, videoId)); | ||
| await setAiProcessingFlag(videoId, false, metadata); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await db() | ||
| .update(videos) | ||
| .set({ | ||
| metadata: { | ||
| ...metadata, | ||
| aiProcessing: true, | ||
| }, | ||
| }) | ||
| .where(eq(videos.id, videoId)); | ||
| const query = await db() | ||
| .select({ video: videos, bucket: s3Buckets }) | ||
| .from(videos) | ||
| .leftJoin(s3Buckets, eq(videos.bucket, s3Buckets.id)) | ||
| .where(eq(videos.id, videoId)); | ||
|
|
||
| if (query.length === 0 || !query[0]) { | ||
| console.error(`[generateAiMetadata] Video data not found for ${videoId}`); | ||
| throw new Error(`Video data not found for ${videoId}`); | ||
| } | ||
| // Set processing flag | ||
| await setAiProcessingFlag(videoId, true, metadata); | ||
|
|
||
| const row = query[0]; | ||
| if (!row || !row.video) { | ||
| console.error( | ||
| `[generateAiMetadata] Video record not found for ${videoId}`, | ||
| ); | ||
| throw new Error(`Video record not found for ${videoId}`); | ||
| } | ||
|
|
||
| const { video } = row; | ||
|
|
||
| const awsBucket = video.awsBucket; | ||
| const awsBucket = videoData.awsBucket; | ||
| if (!awsBucket) { | ||
| console.error( | ||
| `[generateAiMetadata] AWS bucket not found for video ${videoId}`, | ||
| ); | ||
| throw new Error(`AWS bucket not found for video ${videoId}`); | ||
| } | ||
|
|
||
|
Comment on lines
+106
to
113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove hard dependency on deprecated videos.awsBucket This blocks custom buckets/CloudFront paths even when bucketData exists. createBucketProvider(bucketData) already resolves the correct provider. Apply: - const awsBucket = videoData.awsBucket;
- if (!awsBucket) {
- console.error(
- `[generateAiMetadata] AWS bucket not found for video ${videoId}`,
- );
- throw new Error(`AWS bucket not found for video ${videoId}`);
- }
-
const bucket = await createBucketProvider(bucketData);Also applies to: 114-115 🤖 Prompt for AI Agents |
||
| const bucket = await createBucketProvider(row.bucket); | ||
| const bucket = await createBucketProvider(bucketData); | ||
|
|
||
| const transcriptKey = `${userId}/${videoId}/transcription.vtt`; | ||
| const vtt = await bucket.getObject(transcriptKey); | ||
|
|
@@ -172,62 +159,69 @@ ${transcriptText}`; | |
| ); | ||
| // Fallback to OpenAI if Groq fails and OpenAI key exists | ||
| if (serverEnv().OPENAI_API_KEY) { | ||
| const aiRes = await fetch( | ||
| "https://api.openai.com/v1/chat/completions", | ||
| { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${serverEnv().OPENAI_API_KEY}`, | ||
| }, | ||
| body: JSON.stringify({ | ||
| model: "gpt-4o-mini", | ||
| messages: [{ role: "user", content: prompt }], | ||
| }), | ||
| }, | ||
| ); | ||
| if (!aiRes.ok) { | ||
| const errorText = await aiRes.text(); | ||
| console.error( | ||
| `[generateAiMetadata] OpenAI API error: ${aiRes.status} ${errorText}`, | ||
| ); | ||
| throw new Error(`OpenAI API error: ${aiRes.status} ${errorText}`); | ||
| } | ||
| const aiJson = await aiRes.json(); | ||
| content = aiJson.choices?.[0]?.message?.content || "{}"; | ||
| content = await callOpenAI(prompt); | ||
| } else { | ||
| throw groqError; | ||
| } | ||
| } | ||
| } else if (serverEnv().OPENAI_API_KEY) { | ||
| // Use OpenAI if Groq client is not available | ||
| const aiRes = await fetch("https://api.openai.com/v1/chat/completions", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${serverEnv().OPENAI_API_KEY}`, | ||
| }, | ||
| body: JSON.stringify({ | ||
| model: "gpt-4o-mini", | ||
| messages: [{ role: "user", content: prompt }], | ||
| }), | ||
| }); | ||
| if (!aiRes.ok) { | ||
| const errorText = await aiRes.text(); | ||
| console.error( | ||
| `[generateAiMetadata] OpenAI API error: ${aiRes.status} ${errorText}`, | ||
| ); | ||
| throw new Error(`OpenAI API error: ${aiRes.status} ${errorText}`); | ||
| } | ||
| const aiJson = await aiRes.json(); | ||
| content = aiJson.choices?.[0]?.message?.content || "{}"; | ||
| content = await callOpenAI(prompt); | ||
| } | ||
|
|
||
| let data: { | ||
| // Type-safe AI response interface | ||
| interface AIResponse { | ||
| title?: string; | ||
| summary?: string; | ||
| chapters?: { title: string; start: number }[]; | ||
| } = {}; | ||
| } | ||
|
|
||
| // Helper function to validate AI response | ||
| function validateAIResponse(obj: unknown): AIResponse { | ||
| const validated: AIResponse = {}; | ||
|
|
||
| if (typeof obj === "object" && obj !== null) { | ||
| const data = obj as Record<string, unknown>; | ||
|
|
||
| if (typeof data.title === "string" && data.title.trim()) { | ||
| validated.title = data.title.trim(); | ||
| } | ||
|
|
||
| if (typeof data.summary === "string" && data.summary.trim()) { | ||
| validated.summary = data.summary.trim(); | ||
| } | ||
|
|
||
| if (Array.isArray(data.chapters)) { | ||
| const validChapters = data.chapters.filter( | ||
| (chapter: unknown): chapter is { title: string; start: number } => { | ||
| if (typeof chapter !== "object" || chapter === null) { | ||
| return false; | ||
| } | ||
|
|
||
| const chapterObj = chapter as Record<string, unknown>; | ||
| const title = chapterObj.title; | ||
| const start = chapterObj.start; | ||
|
|
||
| return ( | ||
| typeof title === "string" && | ||
| typeof start === "number" && | ||
| title.trim().length > 0 && | ||
| start >= 0 | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| validated.chapters = validChapters.map((chapter) => ({ | ||
| title: chapter.title.trim(), | ||
| start: Math.floor(chapter.start), | ||
| })); | ||
| } | ||
| } | ||
|
|
||
| return validated; | ||
| } | ||
|
|
||
| let data: AIResponse = {}; | ||
| try { | ||
| // Remove markdown code blocks if present | ||
| let cleanContent = content; | ||
|
|
@@ -238,9 +232,19 @@ ${transcriptText}`; | |
| } else if (content.includes("```")) { | ||
| cleanContent = content.replace(/```\s*/g, ""); | ||
| } | ||
| data = JSON.parse(cleanContent.trim()); | ||
|
|
||
| const parsedData = JSON.parse(cleanContent.trim()); | ||
| data = validateAIResponse(parsedData); | ||
|
|
||
| // Log if validation removed invalid data | ||
| if (Object.keys(parsedData).length !== Object.keys(data).length) { | ||
| console.warn( | ||
| `[generateAiMetadata] Some AI response data was invalid and filtered out`, | ||
| ); | ||
| } | ||
| } catch (e) { | ||
| console.error(`[generateAiMetadata] Error parsing AI response: ${e}`); | ||
| console.error(`[generateAiMetadata] Raw content: ${content}`); | ||
| data = { | ||
| title: "Generated Title", | ||
| summary: | ||
|
|
@@ -249,8 +253,7 @@ ${transcriptText}`; | |
| }; | ||
| } | ||
|
|
||
| const currentMetadata: VideoMetadata = | ||
| (video.metadata as VideoMetadata) || {}; | ||
| const currentMetadata: VideoMetadata = metadata; | ||
| const updatedMetadata: VideoMetadata = { | ||
| ...currentMetadata, | ||
| aiTitle: data.title || currentMetadata.aiTitle, | ||
|
|
@@ -259,45 +262,35 @@ ${transcriptText}`; | |
| aiProcessing: false, | ||
| }; | ||
|
|
||
| await db() | ||
| .update(videos) | ||
| .set({ metadata: updatedMetadata }) | ||
| .where(eq(videos.id, videoId)); | ||
|
|
||
| // Batch database updates | ||
| const hasDatePattern = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test( | ||
| video.name || "", | ||
| videoData.name || "", | ||
| ); | ||
|
|
||
| if ( | ||
| (video.name?.startsWith("Cap Recording -") || hasDatePattern) && | ||
| data.title | ||
| ) { | ||
| const shouldUpdateName = | ||
| (videoData.name?.startsWith("Cap Recording -") || hasDatePattern) && | ||
| data.title; | ||
|
|
||
| if (shouldUpdateName) { | ||
| // Update both metadata and name in a single query | ||
| await db() | ||
| .update(videos) | ||
| .set({ | ||
| metadata: updatedMetadata, | ||
| name: data.title, | ||
| }) | ||
| .where(eq(videos.id, videoId)); | ||
| } else { | ||
| // Update only metadata | ||
| await db() | ||
| .update(videos) | ||
| .set({ name: data.title }) | ||
| .set({ metadata: updatedMetadata }) | ||
| .where(eq(videos.id, videoId)); | ||
| } | ||
| } catch (error) { | ||
| console.error(`[generateAiMetadata] Error for video ${videoId}:`, error); | ||
|
|
||
| try { | ||
| const currentVideo = await db() | ||
| .select() | ||
| .from(videos) | ||
| .where(eq(videos.id, videoId)); | ||
| if (currentVideo.length > 0 && currentVideo[0]) { | ||
| const currentMetadata: VideoMetadata = | ||
| (currentVideo[0].metadata as VideoMetadata) || {}; | ||
| await db() | ||
| .update(videos) | ||
| .set({ | ||
| metadata: { | ||
| ...currentMetadata, | ||
| aiProcessing: false, | ||
| }, | ||
| }) | ||
| .where(eq(videos.id, videoId)); | ||
| } | ||
| await setAiProcessingFlag(videoId, false, metadata); | ||
| } catch (updateError) { | ||
| console.error( | ||
| `[generateAiMetadata] Failed to reset processing flag:`, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add timeout/abort handling to OpenAI fetch to avoid hanging requests
External calls should use timeouts; a stuck fetch will leave aiProcessing true longer than needed.
Apply:
📝 Committable suggestion
🤖 Prompt for AI Agents