fix: make chat creation idempotent
This commit is contained in:
parent
7f309aad22
commit
cb8da8b512
@ -4456,9 +4456,27 @@ const SMART_AUTO_GRAPH_TIMEOUT_MS = 28_000;
|
|||||||
const SMART_AUTO_FALLBACK_TIMEOUT_MS = 20_000;
|
const SMART_AUTO_FALLBACK_TIMEOUT_MS = 20_000;
|
||||||
const SMART_AUTO_IMAGE_SUMMARY_TIMEOUT_MS = 18_000;
|
const SMART_AUTO_IMAGE_SUMMARY_TIMEOUT_MS = 18_000;
|
||||||
const DEFAULT_STREAM_KEEPALIVE_INTERVAL_MS = 12_000;
|
const DEFAULT_STREAM_KEEPALIVE_INTERVAL_MS = 12_000;
|
||||||
|
const CHAT_CREATION_LOOKUP_RETRY_DELAYS_MS = [60, 180, 360] as const;
|
||||||
|
|
||||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
async function findChatWithRetry(chatId: string) {
|
||||||
|
let currentChat = await getChatById({ id: chatId });
|
||||||
|
if (currentChat) {
|
||||||
|
return currentChat;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const delayMs of CHAT_CREATION_LOOKUP_RETRY_DELAYS_MS) {
|
||||||
|
await sleep(delayMs);
|
||||||
|
currentChat = await getChatById({ id: chatId });
|
||||||
|
if (currentChat) {
|
||||||
|
return currentChat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
class OperationTimeoutError extends Error {
|
class OperationTimeoutError extends Error {
|
||||||
constructor(operation: string, timeoutMs: number) {
|
constructor(operation: string, timeoutMs: number) {
|
||||||
super(`${operation} timed out after ${timeoutMs}ms`);
|
super(`${operation} timed out after ${timeoutMs}ms`);
|
||||||
@ -6276,7 +6294,7 @@ export async function POST(request: Request) {
|
|||||||
visibility: selectedVisibilityType,
|
visibility: selectedVisibilityType,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const racedChat = await getChatById({ id });
|
const racedChat = await findChatWithRetry(id);
|
||||||
if (!racedChat) {
|
if (!racedChat) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@ -6291,7 +6309,7 @@ export async function POST(request: Request) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
chat = await getChatById({ id });
|
chat = await findChatWithRetry(id);
|
||||||
if (!chat) {
|
if (!chat) {
|
||||||
throw new ChatSDKError("bad_request:database", "Failed to save chat");
|
throw new ChatSDKError("bad_request:database", "Failed to save chat");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -168,13 +168,18 @@ export async function saveChat({
|
|||||||
visibility: VisibilityType;
|
visibility: VisibilityType;
|
||||||
}) {
|
}) {
|
||||||
try {
|
try {
|
||||||
return await db.insert(chat).values({
|
return await db
|
||||||
id,
|
.insert(chat)
|
||||||
createdAt: new Date(),
|
.values({
|
||||||
userId,
|
id,
|
||||||
title,
|
createdAt: new Date(),
|
||||||
visibility,
|
userId,
|
||||||
});
|
title,
|
||||||
|
visibility,
|
||||||
|
})
|
||||||
|
.onConflictDoNothing({
|
||||||
|
target: chat.id,
|
||||||
|
});
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
throw new ChatSDKError("bad_request:database", "Failed to save chat");
|
throw new ChatSDKError("bad_request:database", "Failed to save chat");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user