jargons.dev icon indicating copy to clipboard operation
jargons.dev copied to clipboard

Code Quality: Add timeout handling for jAI service requests

Open babblebey opened this issue 3 months ago • 2 comments

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.

babblebey avatar Sep 30 '25 23:09 babblebey

Hey @babblebey , I want to work on this issue, please assign it to me

mustafa-sayyed avatar Oct 02 '25 11:10 mustafa-sayyed

Hey @mustafa-sayyed

Thank you for your interest in this issue.

I'm afraid we cannot work on this issue yet, as this falls under the "Closed to Community Pick Up" with the presence of the wontfix label.

See the Contribution Guide - Labels Closed to Community Pick Up to learn about these issues.

In the meantime, kindly look through the repo for other issues you can work on 😉

babblebey avatar Oct 02 '25 12:10 babblebey