Repository: DevGruGold/suite
Analysis Date: December 28, 2024
Analyzed By: AI Assistant
Your repository has a well-designed multi-tier AI fallback system with proper Google Cloud OAuth integration. However, there are critical connection gaps between components that need to be addressed.
Frontend (UnifiedChat.tsx)
↓
ai-chat Edge Function (Primary Router)
↓
├── Gemini API (Priority 1)
├── DeepSeek API (Priority 2)
└── Emergency Static Fallback (Priority 3)
vertex-ai-chat Edge Function
↓
google-cloud-auth Edge Function (OAuth Token)
↓
Vertex AI API (gemini-1.5-pro)
Current Situation:
- The
ai-chatfunction (line 163) usescallGeminiFallback()which calls Gemini API directly - It does NOT invoke
vertex-ai-chatEdge Function - Vertex AI Chat exists but is isolated - only called by
toolExecutor.tsfor specific operations
Evidence from Code:
// ai-chat/index.ts Line 163
const geminiResult = await callGeminiFallback(aiMessages, ELIZA_TOOLS);
// This calls Gemini API directly, NOT vertex-ai-chat Edge FunctionImpact:
- ❌ Vertex AI capabilities (with OAuth) are not being used for general chat
- ❌ Google Cloud services integration is bypassed
- ❌ Missing Gmail, Drive, Sheets, Calendar operations in chat flow
✅ CONFIGURED:
// google-cloud-auth/index.ts has full implementation:
- OAuth flow: get_authorization_url → callback → refresh token
- Gmail API: send/read emails, create drafts
- Drive API: upload/download files, create folders
- Sheets API: create/read/write spreadsheets
- Calendar API: create/update/delete events
- Token refresh via oauth_connections tableRequired Environment Variables:
# Supabase Edge Function Secrets (check with `supabase secrets list`)
GOOGLE_CLIENT_ID=your_client_id
GOOGLE_CLIENT_SECRET=your_client_secret
GOOGLE_REFRESH_TOKEN=your_refresh_token # OR stored in oauth_connections table
GOOGLE_CLOUD_PROJECT_ID=your_project_idFallback System: The code checks:
- ✅ Environment variable
GOOGLE_REFRESH_TOKENfirst - ✅ Falls back to
oauth_connectionsdatabase table - ✅ Uses
xmrtsolutions@gmail.comas login hint
Current Activation Points:
// toolExecutor.ts calls vertex-ai-chat for:
- Gmail operations
- Google Drive operations
- Google Sheets operations
- Google Calendar operationsNOT activated for:
- ❌ General chat conversations
- ❌ Image analysis requests
- ❌ Regular AI assistance
Location: supabase/functions/ai-chat/index.ts
Current Code (Line 160-203):
// Try Gemini first (most reliable for general AI)
const geminiResult = await callGeminiFallback(aiMessages, ELIZA_TOOLS);
if (geminiResult) {
// ... handle response
}
// Try DeepSeek fallback
const deepseekResult = await callDeepSeekFallback(aiMessages, ELIZA_TOOLS);RECOMMENDED Change:
// ========== PHASE: AI PROCESSING WITH FALLBACKS ==========
console.log('🚀 Trying AI providers in sequence...');
// 1. Try Vertex AI first (Google Cloud OAuth with Gemini 1.5 Pro)
try {
const vertexResult = await supabase.functions.invoke('vertex-ai-chat', {
body: { messages: aiMessages }
});
if (vertexResult.data?.success) {
console.log('✅ Vertex AI succeeded');
return new Response(
JSON.stringify({
success: true,
response: vertexResult.data.data.choices[0].message.content,
executive: 'ai-chat',
executiveTitle: 'AI Assistant [Vertex AI]',
provider: 'vertex-ai',
model: 'gemini-1.5-pro'
}),
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
);
}
} catch (error) {
console.log('⚠️ Vertex AI failed, falling back to Gemini API:', error);
}
// 2. Try Gemini API directly (fallback)
const geminiResult = await callGeminiFallback(aiMessages, ELIZA_TOOLS);
if (geminiResult) {
// ... existing code
}
// 3. Try DeepSeek fallback
const deepseekResult = await callDeepSeekFallback(aiMessages, ELIZA_TOOLS);Create a Health Check Script:
# Test OAuth configuration
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/google-cloud-auth \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"action":"status"}'
# Expected Response:
{
"success": true,
"configured": {
"client_id": true,
"client_secret": true,
"refresh_token": true
},
"ready": true,
"available_services": ["gmail", "drive", "sheets", "calendar", "gemini"],
"message": "Google Cloud OAuth fully configured..."
}If refresh_token: false:
# 1. Get authorization URL
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/google-cloud-auth \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"action":"get_authorization_url"}'
# 2. Open the returned URL in browser
# 3. Sign in with xmrtsolutions@gmail.com
# 4. Copy the refresh_token from callback response
# 5. Store in Supabase secrets:
supabase secrets set GOOGLE_REFRESH_TOKEN=<your_refresh_token>Location: supabase/functions/_shared/ai-gateway.ts
Add Vertex Provider:
const GATEWAY_CONFIG: GatewayConfig = {
providers: [
{
name: 'vertex',
endpoint: 'https://YOUR_PROJECT.supabase.co/functions/v1/vertex-ai-chat',
model: 'gemini-1.5-pro',
priority: 1, // Highest priority
rateLimit: 2000,
timeout: 30000,
available: true
},
{
name: 'gemini',
endpoint: 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent',
model: 'gemini-1.5-flash',
priority: 2, // Fallback to direct API
rateLimit: 1500,
timeout: 30000,
available: true
},
// ... other providers
],
// ...
};# Run this command to verify:
supabase secrets list
# Required secrets:
□ GOOGLE_CLIENT_ID
□ GOOGLE_CLIENT_SECRET
□ GOOGLE_REFRESH_TOKEN (or in oauth_connections table)
□ GOOGLE_CLOUD_PROJECT_ID
□ SUPABASE_URL
□ SUPABASE_SERVICE_ROLE_KEY-- Check oauth_connections table
SELECT provider, is_active, connected_at, refresh_token IS NOT NULL as has_token
FROM oauth_connections
WHERE provider = 'google_cloud'
ORDER BY connected_at DESC
LIMIT 1;
-- Expected: 1 row with provider='google_cloud', is_active=true, has_token=true# 1. Test google-cloud-auth
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/google-cloud-auth \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"action":"get_access_token"}'
# 2. Test vertex-ai-chat
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/vertex-ai-chat \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Hello, test message"}]}'
# 3. Test ai-chat
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/ai-chat \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Test"}]}'# Step 1: Check Supabase secrets
supabase secrets list
# Step 2: Test OAuth status
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/google-cloud-auth \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"action":"status"}'Expected Output:
{
"success": true,
"configured": {
"client_id": true,
"client_secret": true,
"refresh_token": true
},
"ready": true
}- ✅ Open
supabase/functions/ai-chat/index.ts - ✅ Add Vertex AI as first fallback (see Fix #1 above)
- ✅ Deploy updated function:
supabase functions deploy ai-chat - ✅ Test with frontend chat
- ✅ Open
supabase/functions/_shared/ai-gateway.ts - ✅ Add Vertex provider to GATEWAY_CONFIG (see Fix #3 above)
- ✅ Redeploy all functions that import ai-gateway
User Message → ai-chat → Gemini API Direct → Response
↓ (if fails)
DeepSeek API → Response
User Message → ai-chat → Vertex AI (OAuth) → Gemini 1.5 Pro → Response
↓ (if fails)
Gemini API Direct → Response
↓ (if fails)
DeepSeek API → Response
Advantages:
- ✅ Full Google Cloud integration (Gmail, Drive, Sheets, Calendar)
- ✅ Enterprise-grade authentication via OAuth
- ✅ Better rate limits via Google Cloud
- ✅ Centralized token management
- ✅ Access to Vertex AI exclusive features
-
Never commit OAuth secrets to Git
- ✅ Your
.env.examplecorrectly uses placeholders - ✅ Actual
.envshould be in.gitignore
- ✅ Your
-
Refresh Token Storage
- ✅ Current code supports both:
- Environment variable (GOOGLE_REFRESH_TOKEN)
- Database table (oauth_connections)
- ✅ Recommend using database for production
- ✅ Current code supports both:
-
Token Rotation
- ✅
getAccessToken()automatically refreshes expired tokens - ✅ Refresh tokens stored securely
- ✅
1. Go to: https://console.cloud.google.com/apis/credentials
2. Create OAuth 2.0 Client ID (Web application)
3. Add authorized redirect URI:
https://YOUR_PROJECT.supabase.co/functions/v1/google-cloud-auth
4. Enable APIs:
- Vertex AI API
- Gmail API
- Google Drive API
- Google Sheets API
- Google Calendar API
5. Copy Client ID and Client Secret to Supabase secrets
# Set a secret
supabase secrets set GOOGLE_CLIENT_ID=your_id
# List all secrets (shows names only, not values)
supabase secrets list
# Delete a secret
supabase secrets unset SECRET_NAME- ✅ Google Cloud OAuth: Fully implemented and ready
⚠️ Vertex AI Chat: Exists but not connected to main chat flow- ❌ ai-chat Integration: Missing Vertex AI fallback
- Add Vertex AI to ai-chat fallback chain (5 minutes)
- Verify OAuth secrets are set (2 minutes)
- Test end-to-end flow (10 minutes)
~20 minutes to connect all components properly
Solution:
# Run OAuth flow to get refresh token
curl -X POST https://YOUR_PROJECT.supabase.co/functions/v1/google-cloud-auth \
-H "Authorization: Bearer YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"action":"get_authorization_url"}'
# Then complete OAuth in browser and store refresh_tokenPossible Causes:
- Expired refresh token (needs re-authorization)
- Incorrect Client ID/Secret
- Insufficient scopes enabled
Solution:
# Re-run OAuth flow with all required scopes
# Scopes are defined in google-cloud-auth/index.ts line 21-42Solution:
# Enable Vertex AI API in Google Cloud Console:
# 1. Go to https://console.cloud.google.com/apis/library/aiplatform.googleapis.com
# 2. Click "Enable"
# 3. Wait 2-3 minutes for propagationReport Generated By: AI Analysis System
Next Review: After implementing fixes
Contact: Support team for implementation assistance