-
Notifications
You must be signed in to change notification settings - Fork 55
Youtube_Tool with gemini support #179
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
Open
OmkarBansod02
wants to merge
4
commits into
browseros-ai:main
Choose a base branch
from
OmkarBansod02:feat/youtube-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d34d11f
implement youtube-tool
OmkarBansod02 069de4f
remove extra logs
OmkarBansod02 1997cd4
Merge branch 'main' of https://github.com/OmkarBansod02/BrowserOS-age…
OmkarBansod02 cffdcf8
fix gemini limitation issue by replacing literal with enum
OmkarBansod02 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| import { DynamicStructuredTool } from '@langchain/core/tools' | ||
| import { z } from 'zod' | ||
| import { ExecutionContext } from '@/lib/runtime/ExecutionContext' | ||
| import { PubSubChannel } from '@/lib/pubsub/PubSubChannel' | ||
| import { Logging } from '@/lib/utils/Logging' | ||
| import { LLMSettingsReader } from '@/lib/llm/settings/LLMSettingsReader' | ||
| import { BrowserOSProvider } from '@/lib/llm/settings/browserOSTypes' | ||
|
|
||
| // Input schema | ||
| const YouTubeToolInputSchema = z.object({ | ||
| action: z.enum(['summarize', 'ask', 'transcript']) | ||
| .describe('Action to perform: summarize (get video summary), ask (answer question about video), transcript (get full transcript)'), | ||
| question: z.string().optional() | ||
| .describe('Question to ask about the video (required for "ask" action)'), | ||
| videoUrl: z.string().url().optional() | ||
| .describe('YouTube video URL (auto-detects from current tab if not provided)') | ||
| }) | ||
|
|
||
| type YouTubeToolInput = z.infer<typeof YouTubeToolInputSchema> | ||
|
|
||
| export function YouTubeTool(context: ExecutionContext): DynamicStructuredTool { | ||
| return new DynamicStructuredTool({ | ||
| name: 'youtube_tool', | ||
| description: `Analyze YouTube videos using Gemini's native video understanding. Capabilities: | ||
|
|
||
| • summarize: Get key takeaways and main points from any video | ||
| • ask: Ask specific questions about video content (requires "question" parameter) | ||
| • transcript: Get full video transcript with timestamps | ||
|
|
||
| AUTO-DETECTION: If no videoUrl provided, analyzes the YouTube video in the current tab. | ||
|
|
||
| NOTE: Requires Gemini API key configured in settings. | ||
|
|
||
| EXAMPLES: | ||
| - "Summarize this YouTube video" | ||
| - "What does the video say about artificial intelligence?" | ||
| - "Get the full transcript of this video"`, | ||
|
|
||
| schema: YouTubeToolInputSchema, | ||
|
|
||
| func: async (args: YouTubeToolInput) => { | ||
| try { | ||
| context.incrementMetric('toolCalls') | ||
|
|
||
| // Check if Gemini is available | ||
| const geminiAvailable = await _isGeminiAvailable() | ||
| if (!geminiAvailable) { | ||
| return JSON.stringify({ | ||
| ok: false, | ||
| error: 'YouTube video analysis requires Gemini API. Please configure a Gemini API key in settings to use this feature.' | ||
| }) | ||
| } | ||
|
|
||
| // Get video URL | ||
| context.getPubSub().publishMessage( | ||
| PubSubChannel.createMessage('Analyzing YouTube video...', 'thinking') | ||
| ) | ||
|
|
||
| let videoUrl = args.videoUrl | ||
| if (!videoUrl) { | ||
| const page = await context.browserContext.getCurrentPage() | ||
| videoUrl = page.url() | ||
| } | ||
|
|
||
| // Validate YouTube URL | ||
| const videoId = _extractVideoId(videoUrl) | ||
| if (!videoId) { | ||
| return JSON.stringify({ | ||
| ok: false, | ||
| error: 'Not a valid YouTube video URL. Please navigate to a YouTube video or provide a video URL.' | ||
| }) | ||
| } | ||
|
|
||
| // Process with Gemini | ||
| return await _processWithGemini(videoUrl, args, context) | ||
|
|
||
| } catch (error) { | ||
| context.incrementMetric('errors') | ||
| Logging.log('YouTubeTool', `Error: ${error instanceof Error ? error.message : String(error)}`, 'error') | ||
| return JSON.stringify({ | ||
| ok: false, | ||
| error: `YouTube tool failed: ${error instanceof Error ? error.message : String(error)}` | ||
| }) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // ============= Helper Functions ============= | ||
|
|
||
| /** | ||
| * Check if Gemini is available and configured | ||
| */ | ||
| async function _isGeminiAvailable(): Promise<boolean> { | ||
| try { | ||
| const config = await LLMSettingsReader.readAllProviders() | ||
| const geminiProvider = config.providers.find((p: BrowserOSProvider) => p.type === 'google_gemini' && p.apiKey) | ||
| return !!geminiProvider | ||
| } catch (error) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Process YouTube video using Gemini's native support via REST API | ||
| */ | ||
| async function _processWithGemini( | ||
| videoUrl: string, | ||
| args: YouTubeToolInput, | ||
| context: ExecutionContext | ||
| ): Promise<string> { | ||
| try { | ||
| // Get Gemini configuration | ||
| const config = await LLMSettingsReader.readAllProviders() | ||
| const geminiProvider = config.providers.find((p: BrowserOSProvider) => p.type === 'google_gemini' && p.apiKey) | ||
|
|
||
| if (!geminiProvider || !geminiProvider.apiKey) { | ||
| throw new Error('Gemini API key not found') | ||
| } | ||
|
|
||
| // Prepare the prompt based on action | ||
| let promptText: string | ||
| switch (args.action) { | ||
| case 'summarize': | ||
| promptText = `Please summarize this YouTube video in 3 sections: | ||
| 1. **Main Topic**: What is this video about? (1-2 sentences) | ||
| 2. **Key Takeaways**: The most important points (3-5 bullet points) | ||
| 3. **Notable Insights**: Any particularly interesting or valuable information` | ||
| break | ||
|
|
||
| case 'ask': | ||
| if (!args.question) { | ||
| return JSON.stringify({ | ||
| ok: false, | ||
| error: 'Question is required for "ask" action. Please provide a question parameter.' | ||
| }) | ||
| } | ||
| promptText = `Answer this question about the YouTube video: "${args.question}" | ||
|
|
||
| Provide a clear, direct answer based on the video content. If the answer includes specific quotes or references, include them.` | ||
| break | ||
|
|
||
| case 'transcript': | ||
| promptText = `Extract and provide the full transcript of this YouTube video with timestamps if available.` | ||
| break | ||
|
|
||
| default: | ||
| throw new Error('Invalid action') | ||
| } | ||
|
|
||
| context.getPubSub().publishMessage( | ||
| PubSubChannel.createMessage('Processing with Gemini...', 'thinking') | ||
| ) | ||
|
|
||
| // Use Gemini REST API directly with proper fileData format | ||
| const modelId = geminiProvider.modelId || 'gemini-2.5-flash' | ||
| const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/${modelId}:generateContent` | ||
|
|
||
| const requestBody = { | ||
| contents: [{ | ||
| parts: [ | ||
| { text: promptText }, | ||
| { file_data: { file_uri: videoUrl } } | ||
| ] | ||
| }], | ||
| generationConfig: { | ||
| temperature: 0.3, | ||
| maxOutputTokens: 2000 | ||
| } | ||
| } | ||
|
|
||
| const response = await fetch(apiUrl, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'x-goog-api-key': geminiProvider.apiKey | ||
| }, | ||
| body: JSON.stringify(requestBody) | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text() | ||
| Logging.log('YouTubeTool', `Gemini API error: ${response.status}`, 'error') | ||
| throw new Error(`Gemini API error: ${response.status} - ${errorText}`) | ||
| } | ||
|
|
||
| const data = await response.json() | ||
|
|
||
| // Extract the text from Gemini's response | ||
| const responseText = data.candidates?.[0]?.content?.parts?.[0]?.text | ||
|
|
||
| if (!responseText) { | ||
| throw new Error('No text content in Gemini response') | ||
| } | ||
|
|
||
| return JSON.stringify({ | ||
| ok: true, | ||
| output: args.action === 'transcript' | ||
| ? { transcript: responseText } | ||
| : args.action === 'ask' | ||
| ? { answer: responseText } | ||
| : { summary: responseText } | ||
| }) | ||
|
|
||
| } catch (error) { | ||
| Logging.log('YouTubeTool', `Gemini processing error: ${error}`, 'error') | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extract video ID from various YouTube URL formats | ||
| */ | ||
| function _extractVideoId(url: string): string | null { | ||
| try { | ||
| // youtube.com/watch?v=VIDEO_ID | ||
| const watchMatch = url.match(/[?&]v=([^&]+)/) | ||
| if (watchMatch) return watchMatch[1] | ||
|
|
||
| // youtu.be/VIDEO_ID | ||
| const shortMatch = url.match(/youtu\.be\/([^?&]+)/) | ||
| if (shortMatch) return shortMatch[1] | ||
|
|
||
| // youtube.com/embed/VIDEO_ID | ||
| const embedMatch = url.match(/youtube\.com\/embed\/([^?&]+)/) | ||
| if (embedMatch) return embedMatch[1] | ||
|
|
||
| // youtube.com/v/VIDEO_ID | ||
| const vMatch = url.match(/youtube\.com\/v\/([^?&]+)/) | ||
| if (vMatch) return vMatch[1] | ||
|
|
||
| // youtube.com/shorts/VIDEO_ID | ||
| const shortsMatch = url.match(/youtube\.com\/shorts\/([^?&]+)/) | ||
| if (shortsMatch) return shortsMatch[1] | ||
|
|
||
| return null | ||
| } catch (error) { | ||
| Logging.log('YouTubeTool', `Error extracting video ID: ${error}`, 'error') | ||
| return null | ||
| } | ||
| } | ||
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
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.
logic: incorrect
file_dataformat - Gemini API expects YouTube URLs to be passed differentlyThe current code passes the YouTube URL directly as
file_uri, but Gemini's API doesn't accept YouTube URLs in thefile_data.file_urifield. That field is for files uploaded via the File API. For YouTube videos, you should pass the URL directly in a text part or use a different approach.Check Gemini documentation for correct YouTube video analysis format.
Prompt To Fix With AI