-
-
Notifications
You must be signed in to change notification settings - Fork 44
Open
Labels
↗️ medium priorityThis issue is crucialThis issue is crucial🔴 wontfixThis will not be worked on for nowThis will not be worked on for now✨ enhancementNew feature or request or improvementNew feature or request or improvementbackendChanges related to apisChanges related to apis✨jaiIssues, PRs or questions related to the ✨jAI moduleIssues, PRs or questions related to the ✨jAI module
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
// 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
- Add a timeout promise (e.g., 30 seconds).
- Use
Promise.raceto race the AI call and the timeout. - Return 504 with a helpful message if timeout occurs.
- 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.
Metadata
Metadata
Assignees
Labels
↗️ medium priorityThis issue is crucialThis issue is crucial🔴 wontfixThis will not be worked on for nowThis will not be worked on for now✨ enhancementNew feature or request or improvementNew feature or request or improvementbackendChanges related to apisChanges related to apis✨jaiIssues, PRs or questions related to the ✨jAI moduleIssues, PRs or questions related to the ✨jAI module