Skip to content

Latest commit

 

History

History
206 lines (152 loc) · 6.89 KB

File metadata and controls

206 lines (152 loc) · 6.89 KB

Fixes Applied - Application Issues

This document summarizes all fixes applied and actions required.


Issue 1: File Upload 500 Internal Server Error - FIXED

Problem

The file upload endpoint /agent/chat/generate-upload-url returned 500 error due to two issues:

  1. Missing JWT authentication dependency
  2. Storage backend wasn't reading from settings (was using hardcoded defaults)

Fix Applied

Part 1: JWT Authentication Added dependencies=[DependsJwtAuth] to the following endpoints in backend/app/agent/api/v1/files.py:

  1. generate_upload_url (line 297)
  2. upload_complete (line 333)
  3. direct_upload (line 374)
  4. direct_upload_form_data (line 385)
  5. multiple_upload (line 394)
  6. get_uploaded_files_list (line 407)

Part 2: S3 Storage Configuration

  1. Updated backend/core/conf.py:

    • Changed default FILE_STORAGE_BACKEND from 'local' to 's3' (production default)
  2. Updated backend/src/services/file_processing/storage.py:

    • get_storage_backend() now properly reads settings from backend.core.conf
    • S3 is the default storage backend (uses Cloudflare R2 configured in .env)
    • Properly initializes S3FileStorage with credentials from settings
  3. Upload flow (S3):

    • Frontend calls POST /agent/chat/generate-upload-url
    • Backend returns presigned S3 URL from Cloudflare R2
    • Frontend PUTs file directly to the presigned URL
    • Frontend calls POST /agent/chat/upload-complete to finalize

Status: ✅ COMPLETE - No user action required


Issue 2: Chat Sessions 404 Not Found - DIAGNOSTIC SCRIPTS PROVIDED

Problem

Chat sessions endpoint returns 404 when trying to fetch session details.

Root Cause Analysis

The 404 occurs when:

  1. The session doesn't exist in the database
  2. The session exists but belongs to a different user (security feature)
  3. The session hasn't been created yet (timing issue)

Diagnostic Script Created

File: DIAGNOSTIC_SQL_SCRIPTS.sql

Run these queries in Supabase SQL Editor to diagnose:

  1. Quick Summary - Run the last query first to see overall database state
  2. Check Tables Exist - Verify agent_chat_sessions table exists
  3. View Your Sessions - Find your user_id and check if sessions are created

Key Queries to Run First:

-- 1. Check if tables exist
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public' AND table_name LIKE 'agent_%';

-- 2. Count records
SELECT COUNT(*) FROM agent_chat_sessions;
SELECT COUNT(*) FROM sys_user;

-- 3. View recent sessions
SELECT uuid, user_id, name, status, created_time
FROM agent_chat_sessions
ORDER BY created_time DESC
LIMIT 10;

Possible Issues to Check:

  1. Table doesn't exist → Run Alembic migrations: alembic upgrade head
  2. No sessions in table → Sessions aren't being created on chat start
  3. Sessions exist but wrong user_id → Check user authentication is correct

Status: ⚠️ REQUIRES INVESTIGATION - Run diagnostic scripts


Issue 3: Google Drive OAuth Error 400 - FIXED

Problem

Google Drive OAuth returned redirect_uri_mismatch error because the .env had an incorrect redirect URI path that didn't exist.

Root Cause

The .env file had:

OAUTH2_GOOGLE_REDIRECT_URI='http://localhost:8000/auth/oauth/google/callback'

But /auth/oauth/google/callback doesn't exist on the backend!

Fix Applied

  1. Added new setting in backend/core/conf.py:

    OAUTH2_GOOGLE_DRIVE_REDIRECT_URI: str = 'http://localhost:1420/google-drive-callback'
  2. Updated connector in backend/app/agent/api/v1/connectors.py:

    • Now uses OAUTH2_GOOGLE_DRIVE_REDIRECT_URI for Google Drive OAuth
    • Falls back to OAUTH2_GOOGLE_REDIRECT_URI if not set
  3. Updated .env:

    # For Google Login (redirect to backend OAuth plugin)
    OAUTH2_GOOGLE_REDIRECT_URI='http://localhost:8000/api/v1/oauth2/google/callback'
    # For Google Drive Connector (redirect to frontend)
    OAUTH2_GOOGLE_DRIVE_REDIRECT_URI='http://localhost:1420/google-drive-callback'

Action Required: Add Redirect URI to Google Cloud Console

  1. Go to Google Cloud Console
  2. Navigate to APIs & ServicesCredentials
  3. Click on your OAuth 2.0 Client ID
  4. Add these Authorized redirect URIs:
    http://localhost:1420/google-drive-callback
    http://localhost:8000/api/v1/oauth2/google/callback
    
  5. Save changes

Status: ✅ CODE FIXED - User needs to update Google Cloud Console


Issue 4: Agent Mode "Not Authenticated" + Infinite Spinner - FIXED

Problem

WebSocket authentication silently failed, causing "Not authenticated" error and infinite spinner.

Fix Applied

  1. Added error handling to save_session() in backend/common/socketio/server.py:

    try:
        await sio.save_session(sid, {...}, namespace='/ws')
    except Exception as e:
        log.error(f'WebSocket session save failed: {e}')
        return False  # Reject connection
  2. Added idle status emission in backend/common/socketio/handlers.py:

    • When auth fails in join_session, now emits status: 'idle' to stop spinner
    • When auth fails in chat_message, now emits status: 'idle' to stop spinner

Status: ✅ COMPLETE - No user action required


Summary of Files Modified

File Changes
backend/app/agent/api/v1/files.py Added JWT auth to 6 endpoints
backend/src/services/file_processing/storage.py Updated get_storage_backend() to read from settings, S3 as default
backend/core/conf.py Changed FILE_STORAGE_BACKEND default to 's3', added OAUTH2_GOOGLE_DRIVE_REDIRECT_URI
backend/common/socketio/server.py Added error handling for save_session
backend/common/socketio/handlers.py Added idle status on auth failure
backend/app/agent/api/v1/connectors.py Updated to use new redirect URI setting
backend/.env Fixed OAuth redirect URIs

After Applying These Fixes

  1. Restart the backend server to pick up the changes
  2. Run the diagnostic SQL scripts to check database state
  3. Update Google Cloud Console with the correct redirect URIs
  4. Test each feature:
    • File upload
    • Chat sessions list
    • Google Drive connection
    • Agent mode chat

Troubleshooting

If Chat Sessions Still Shows 404:

  1. Check browser Network tab for the exact URL being requested
  2. Verify JWT token is being sent (Authorization header)
  3. Run SQL scripts to see if sessions exist for your user

If Google Drive Still Fails:

  1. Wait a few minutes after updating Google Cloud Console (propagation delay)
  2. Clear browser cookies and try again
  3. Check browser console for detailed error messages

If Agent Mode Still Fails:

  1. Check Docker logs: docker compose logs backend --tail=100
  2. Verify Redis is running: docker compose ps
  3. Check if WebSocket connection is established (Network → WS tab)