Skip to content

Code Quality: Add timeout handling for jAI service requests #221

Description

@babblebey

Problem

The jAI search API endpoint (src/pages/api/jai/search.js) does not currently implement a timeout for requests to the AI service. If the AI service is slow or unresponsive, the API may hang indefinitely, leading to poor user experience.

Current Behavior

  • No timeout is set for the AI service call.
  • If the AI service is slow or fails to respond, the API request may hang.

Expected Behavior

  • The API should set a reasonable timeout (e.g., 30 seconds) for the AI service call.
  • If the timeout is reached, the API should return a 504 Gateway Timeout error with a helpful message.

Location

File: src/pages/api/jai/search.js

Proposed Implementation

// Add timeout to the stream processing
const timeoutMs = 30000; // 30 seconds
const timeoutPromise = new Promise((_, reject) => {
  setTimeout(() => reject(new Error('Request timeout')), timeoutMs);
});
try {
  const streamPromise = chain.stream({
    question: currentMessageContent,
  });
  const stream = await Promise.race([streamPromise, timeoutPromise]);
  // ...existing stream processing logic...
} catch (e) {
  if (e.message === 'Request timeout') {
    return Response.json(
      { error: 'Request timeout', details: 'The AI service took too long to respond' },
      { status: 504, headers: corsHeaders }
    );
  }
  throw e; // Re-throw for other error handling
}

Steps to Complete

  1. Add a timeout promise (e.g., 30 seconds).
  2. Use Promise.race to race the AI call and the timeout.
  3. Return 504 with a helpful message if timeout occurs.
  4. Test with simulated slow AI service.

Definition of Done

  • Timeout logic is implemented for the AI service call.
  • 504 error is returned if timeout is reached.
  • CORS headers are included in timeout responses.
  • Existing functionality for fast responses remains intact.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ↗️ medium priorityThis issue is crucial🔴 wontfixThis will not be worked on for now✨ enhancementNew feature or request or improvementbackendChanges related to apis✨jaiIssues, PRs or questions related to the ✨jAI module

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions