fix: race condition after resumable stream ends (#986)

This commit is contained in:
Jeremy 2025-05-07 16:02:53 -07:00 committed by GitHub
parent 566b01f367
commit 75af1320f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 145 additions and 18 deletions

View file

@ -34,6 +34,7 @@ import {
} from 'resumable-stream';
import { after } from 'next/server';
import type { Chat } from '@/lib/db/schema';
import { differenceInSeconds } from 'date-fns';
export const maxDuration = 60;
@ -245,6 +246,7 @@ export async function POST(request: Request) {
export async function GET(request: Request) {
const streamContext = getStreamContext();
const resumeRequestedAt = new Date();
if (!streamContext) {
return new Response(null, { status: 204 });
@ -295,12 +297,46 @@ export async function GET(request: Request) {
execute: () => {},
});
return new Response(
await streamContext.resumableStream(recentStreamId, () => emptyDataStream),
{
status: 200,
},
const stream = await streamContext.resumableStream(
recentStreamId,
() => emptyDataStream,
);
/*
* For when the generation is streaming during SSR
* but the resumable stream has concluded at this point.
*/
if (!stream) {
const messages = await getMessagesByChatId({ id: chatId });
const mostRecentMessage = messages.at(-1);
if (!mostRecentMessage) {
return new Response(emptyDataStream, { status: 200 });
}
if (mostRecentMessage.role !== 'assistant') {
return new Response(emptyDataStream, { status: 200 });
}
const messageCreatedAt = new Date(mostRecentMessage.createdAt);
if (differenceInSeconds(resumeRequestedAt, messageCreatedAt) > 15) {
return new Response(emptyDataStream, { status: 200 });
}
const restoredStream = createDataStream({
execute: (buffer) => {
buffer.writeData({
type: 'append-message',
message: JSON.stringify(mostRecentMessage),
});
},
});
return new Response(restoredStream, { status: 200 });
}
return new Response(stream, { status: 200 });
}
export async function DELETE(request: Request) {