Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
getChatById,
getMessageCountByUserId,
getMessagesByChatId,
getStreamIdsByChatId,
saveChat,
saveMessages,
updateChatTitleById,
Expand All @@ -57,6 +58,63 @@ function getStreamContext() {

export { getStreamContext };

export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const chatId = searchParams.get("chatId");

if (!chatId) {
return new ChatbotError("bad_request:api").toResponse();
}

const session = await auth();

if (!session?.user) {
return new ChatbotError("unauthorized:chat").toResponse();
}

const chat = await getChatById({ id: chatId });

if (!chat) {
return new Response(null, { status: 204 });
}

if (chat.userId !== session.user.id) {
return new ChatbotError("forbidden:chat").toResponse();
}

const streamContext = getStreamContext();

if (!streamContext) {
return new Response(null, { status: 204 });
}

const streamIds = await getStreamIdsByChatId({ chatId });

if (!streamIds.length) {
return new Response(null, { status: 204 });
}

const recentStreamId = streamIds.at(-1);

if (!recentStreamId) {
return new Response(null, { status: 204 });
}

const emptyStream = new ReadableStream({
start(controller) {
controller.close();
},
});

return new Response(
await streamContext.resumeExistingStream(recentStreamId, emptyStream),
Comment on lines +103 to +110
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const emptyStream = new ReadableStream({
start(controller) {
controller.close();
},
});
return new Response(
await streamContext.resumeExistingStream(recentStreamId, emptyStream),
return new Response(
await streamContext.resumeExistingStream(recentStreamId),

resumeExistingStream is called with a ReadableStream as the second argument where a number (skipCharacters) is expected, causing the stream to be coerced to NaN.

Fix on Vercel

{
status: 200,
headers: { "Content-Type": "text/event-stream" },
}
);
}

export async function POST(request: Request) {
let requestBody: PostRequestBody;

Expand Down
16 changes: 16 additions & 0 deletions tests/e2e/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ test.describe("Chat Error Handling", () => {
});
});

test.describe("Resumable Streams", () => {
test("GET /api/chat returns 400 when chatId is missing", async ({
request,
}) => {
const response = await request.get("/api/chat");
expect(response.status()).toBe(400);
});

test("GET /api/chat returns 401 when not authenticated", async ({
request,
}) => {
const response = await request.get("/api/chat?chatId=nonexistent");
expect(response.status()).toBe(401);
});
});

test.describe("Suggested Actions", () => {
test("suggested actions are clickable", async ({ page }) => {
await page.goto("/");
Expand Down