Good. Now add the 6 new API endpoints before the `default` case in v1.


  5392→            $taskId = $_POST['task_id'] ?? $_GET['task_id'] ?? '';
  5393→            if (empty($taskId)) {
  5394→                jsonError('Task ID is required', 400);
  5395→            }
  5396→
  5397→            $mongo->update('extension_scrape_queue',
  5398→                ['_id' => new \MongoDB\BSON\ObjectId($taskId)],
  5399→                ['status' => 'completed', 'completed_at' => date('Y-m-d H:i:s')]
  5400→            );
  5401→
  5402→            jsonResponse(['success' => true]);
  5403→            break;
  5404→
  5405→        // ==========================================
  5406→        // Default
  5407→        // ==========================================
  5408→        default:
  5409→            jsonError('Unknown action: ' . $action, 400);
  5410→    }
  5411→

            $taskId = $_POST['task_id'] ?? $_GET['task_id'] ?? '';
            if (empty($taskId)) {
                jsonError('Task ID is required', 400);
            }

            $mongo->update('extension_scrape_queue',
                ['_id' => new \MongoDB\BSON\ObjectId($taskId)],
                ['status' => 'completed', 'completed_at' => date('Y-m-d H:i:s')]
            );

            jsonResponse(['success' => true]);
            break;

        // ==========================================
        // Default
        // ==========================================
        default:
            jsonError('Unknown action: ' . $action, 400);
    }


Edit

        // ==========================================
        // AI Chat Sessions - List recent sessions
        // ==========================================
        case 'ai_chat_sessions':
            $ctxMongo = \EmailIntelligence\MongoDatabase::getInstance();
            $limit = (int)($_GET['limit'] ?? 20);
            $sessions = $ctxMongo->findAll('ai_chat_sessions', [], [
                'sort' => ['updated_at' => -1],
                'limit' => $limit,
            ]);
            // Strip full messages from list view (just return metadata)
            $sessionList = array_map(function($s) {
                return [
                    'session_id' => $s['session_id'] ?? '',
                    'title' => $s['title'] ?? 'Untitled',
                    'agent' => $s['agent'] ?? 'general',
                    'provider' => $s['provider'] ?? '',
                    'model' => $s['model'] ?? '',
                    'contact_name' => $s['contact_name'] ?? null,
                    'contact_id' => $s['contact_id'] ?? null,
                    'context_thread_ids' => $s['context_thread_ids'] ?? [],
                    'message_count' => count($s['messages'] ?? []),
                    'created_at' => $s['created_at'] ?? '',
                    'updated_at' => $s['updated_at'] ?? '',
                ];
            }, $sessions);
            jsonResponse(['success' => true, 'sessions' => $sessionList]);
            break;

        // ==========================================
        // AI Chat Session - Load a specific session
        // ==========================================
        case 'ai_chat_session':
            $sessionId = $_GET['session_id'] ?? '';
            if (!$sessionId) jsonError('session_id required');
            $ctxMongo = \EmailIntelligence\MongoDatabase::getInstance();
            $session = $ctxMongo->findOne('ai_chat_sessions', ['session_id' => $sessionId]);
            if (!$session) jsonError('Session not found', 404);
            jsonResponse(['success' => true, 'session' => [
                'session_id' => $session['session_id'],
                'title' => $session['title'] ?? 'Untitled',
                'agent' => $session['agent'] ?? 'general',
                'provider' => $session['provider'] ?? '',
                'model' => $session['model'] ?? '',
                'contact_id' => $session['contact_id'] ?? null,
                'contact_name' => $session['contact_name'] ?? null,
                'context_thread_ids' => $session['context_thread_ids'] ?? [],
                'messages' => $session['messages'] ?? [],
                'created_at' => $session['created_at'] ?? '',
                'updated_at' => $session['updated_at'] ?? '',
            ]]);
            break;

        // ==========================================
        // AI Chat Session Delete
        // ==========================================
        case 'ai_chat_session_delete':
            if ($_SERVER['REQUEST_METHOD'] !== 'POST') jsonError('POST required', 405);
            $jsonData = json_decode(file_get_contents('php://input'), true) ?? [];
            $sessionId = $jsonData['session_id'] ?? '';
            if (!$sessionId) jsonError('session_id required');
            $ctxMongo = \EmailIntelligence\MongoDatabase::getInstance();
            $deleted = $ctxMongo->delete('ai_chat_sessions', ['session_id' => $sessionId]);
            jsonResponse(['success' => true, 'deleted' => $deleted]);
            break;

        // ==========================================
        // AI Contact Memories - Get memories for a contact
        // ==========================================
        case 'ai_contact_memories':
            $contactId = (int)($_GET['contact_id'] ?? 0);
            if (!$contactId) jsonError('contact_id required');
            $ctxMongo = \EmailIntelligence\MongoDatabase::getInstance();
            $memDoc = $ctxMongo->findOne('ai_contact_memory', ['contact_id' => $contactId]);
            jsonResponse([
                'success' => true,
                'contact_id' => $contactId,
                'memories' => $memDoc ? ($memDoc['memories'] ?? []) : [],
            ]);
            break;

        // ==========================================
        // AI Contact Memory Add - Manually add a memory fact
        // ==========================================
        case 'ai_contact_memory_add':
            if ($_SERVER['REQUEST_METHOD'] !== 'POST') jsonError('POST required', 405);
            $jsonData = json_decode(file_get_contents('php://input'), true) ?? [];
            $contactId = (int)($jsonData['contact_id'] ?? 0);
            $fact = trim($jsonData['fact'] ?? '');
            if (!$contactId || !$fact) jsonError('contact_id and fact required');
            $ctxMongo = \EmailIntelligence\MongoDatabase::getInstance();
            $existing = $ctxMongo->findOne('ai_contact_memory', ['contact_id' => $contactId]);
            $memories = $existing ? ($existing['memories'] ?? []) : [];
            $newMem = [
                'id' => uniqid('mem_', true),
                'fact' => $fact,
                'source' => 'manual',
                'created_at' => date('Y-m-d H:i:s'),
            ];
            $memories[] = $newMem;
            $ctxMongo->insertOrUpdate('ai_contact_memory', [
                'contact_id' => $contactId,
                'memories' => $memories,
                'updated_at' => date('Y-m-d H:i:s'),
            ], ['contact_id']);
            jsonResponse(['success' => true, 'memory' => $newMem]);
            break;

        // ==========================================
        // AI Contact Memory Delete - Remove a memory fact
        // ==========================================
        case 'ai_contact_memory_delete':
            if ($_SERVER['REQUEST_METHOD'] !== 'POST') jsonError('POST required', 405);
            $jsonData = json_decode(file_get_contents('php://input'), true) ?? [];
            $contactId = (int)($jsonData['contact_id'] ?? 0);
            $memoryId = $jsonData['memory_id'] ?? '';
            if (!$contactId || !$memoryId) jsonError('contact_id and memory_id required');
            $ctxMongo = \EmailIntelligence\MongoDatabase::getInstance();
            $existing = $ctxMongo->findOne('ai_contact_memory', ['contact_id' => $contactId]);
            if ($existing && !empty($existing['memories'])) {
                $memories = array_values(array_filter($existing['memories'], function($m) use ($memoryId) {
                    return ($m['id'] ?? '') !== $memoryId;
                }));
                $ctxMongo->insertOrUpdate('ai_contact_memory', [
                    'contact_id' => $contactId,
                    'memories' => $memories,
                    'updated_at' => date('Y-m-d H:i:s'),
                ], ['contact_id']);
            }
            jsonResponse(['success' => true]);
            break;

        // ==========================================
        // Default
        // ==========================================
        default:
            jsonError('Unknown action: ' . $action, 400);
    }