jargonsdev / jargonsdev/jargons.dev

Code Quality: Add timeout handling for jAI service requests

Open
#221 2 comments 0 reactions 0 assignees View on GitHub
:arrow_upper_right: medium priority :red_circle: wontfix :sparkles: enhancement ✨jai backend
Dominant language
MDX
Stars
56
Forks
45
PR merge metrics
No merged PRs in 30d

Description

## **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**
```javascript
// 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.

Contributor guide

Open the contributing guide

Research direction

Start with src/pages/api/jai/search.js and inspect the existing chain.stream call, stream processing, and error handling. Test the endpoint with a simulated slow AI service, then verify that timeout responses return status 504 with the expected message and CORS headers while fast responses remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.