mirror of https://github.com/buster-so/buster.git
Add 10 second timeout generating new prompts before returning old ones
This commit is contained in:
parent
b546d121fd
commit
6069c24061
|
@ -22,7 +22,6 @@ const app = new Hono().get(
|
||||||
async (c) => {
|
async (c) => {
|
||||||
try {
|
try {
|
||||||
const userId = c.req.param('id');
|
const userId = c.req.param('id');
|
||||||
|
|
||||||
const authenticatedUser = c.get('busterUser');
|
const authenticatedUser = c.get('busterUser');
|
||||||
|
|
||||||
// Authorization check: Users can only access their own suggested prompts
|
// Authorization check: Users can only access their own suggested prompts
|
||||||
|
@ -34,16 +33,7 @@ const app = new Hono().get(
|
||||||
|
|
||||||
const currentSuggestedPrompts = await getUserSuggestedPrompts({ userId });
|
const currentSuggestedPrompts = await getUserSuggestedPrompts({ userId });
|
||||||
|
|
||||||
// If no prompts exist, try to generate new ones or return defaults
|
if (currentSuggestedPrompts) {
|
||||||
if (!currentSuggestedPrompts) {
|
|
||||||
try {
|
|
||||||
const newPrompts = await buildNewSuggestedPrompts(userId);
|
|
||||||
return c.json(newPrompts);
|
|
||||||
} catch {
|
|
||||||
return c.json(DEFAULT_USER_SUGGESTED_PROMPTS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the updatedAt date is from today
|
// Check if the updatedAt date is from today
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
const updatedDate = new Date(currentSuggestedPrompts.updatedAt);
|
const updatedDate = new Date(currentSuggestedPrompts.updatedAt);
|
||||||
|
@ -53,19 +43,26 @@ const app = new Hono().get(
|
||||||
today.getMonth() === updatedDate.getMonth() &&
|
today.getMonth() === updatedDate.getMonth() &&
|
||||||
today.getDate() === updatedDate.getDate();
|
today.getDate() === updatedDate.getDate();
|
||||||
|
|
||||||
// If prompts are from today, return them
|
|
||||||
if (isToday) {
|
if (isToday) {
|
||||||
return c.json(currentSuggestedPrompts);
|
return c.json(currentSuggestedPrompts);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeoutMs = 10000; // 10 seconds timeout
|
||||||
|
|
||||||
|
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('Request timeout after 10 seconds. Returning current suggested prompts.'));
|
||||||
|
}, timeoutMs);
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const newPrompts = await buildNewSuggestedPrompts(userId);
|
const newPrompts = await Promise.race([buildNewSuggestedPrompts(userId), timeoutPromise]);
|
||||||
return c.json(newPrompts);
|
return c.json(newPrompts);
|
||||||
} catch {
|
} catch {
|
||||||
if (currentSuggestedPrompts) {
|
if (currentSuggestedPrompts) {
|
||||||
return c.json(currentSuggestedPrompts);
|
return c.json(currentSuggestedPrompts);
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.json(DEFAULT_USER_SUGGESTED_PROMPTS);
|
return c.json(DEFAULT_USER_SUGGESTED_PROMPTS);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import { openai } from '@ai-sdk/openai';
|
import { DEFAULT_USER_SUGGESTED_PROMPTS } from '@buster/database';
|
||||||
import { DEFAULT_USER_SUGGESTED_PROMPTS, type UserSuggestedPromptsField } from '@buster/database';
|
|
||||||
import { generateObject } from 'ai';
|
import { generateObject } from 'ai';
|
||||||
import { wrapTraced } from 'braintrust';
|
import { wrapTraced } from 'braintrust';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
Loading…
Reference in New Issue