An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix(streaming): build the prompt from an uncached chat read (#1001)
Yoshiki Miurac668c34843ba
diff --git a/lib/actions/chat.ts b/lib/actions/chat.tsindex e0171297..d198c727 100644--- a/lib/actions/chat.ts+++ b/lib/actions/chat.ts@@ -34,6 +34,17 @@ const getCachedChatWithMessages = (return cachedFunction()}+async function signChatFilePartUrls(+ chat: (Chat & { messages: UIMessage[] }) | null+): Promise<(Chat & { messages: UIMessage[] }) | null> {+ if (!chat) return null++ return {+ ...chat,+ messages: await signFilePartUrlsInMessages(chat.messages)+ }+}+/*** Get all chats for the current user*/@@ -67,12 +78,23 @@ export async function loadChat(): Promise<(Chat & { messages: UIMessage[] }) | null> {// Use cached version for individual chat loadingconst chat = await getCachedChatWithMessages(chatId, requestingUserId)- if (!chat) return null- return {- ...chat,- messages: await signFilePartUrlsInMessages(chat.messages)- }+ return signChatFilePartUrls(chat)+}++/**+ * Load a chat bypassing the request cache.+ * The assistant answer is persisted after the streaming response has been+ * returned, so its revalidateTag never reaches the cache and a cached read+ * can miss the previous turn's answer.+ */+export async function loadChatUncached(+ chatId: string,+ requestingUserId?: string+): Promise<(Chat & { messages: UIMessage[] }) | null> {+ const chat = await dbActions.loadChatWithMessages(chatId, requestingUserId)++ return signChatFilePartUrls(chat)}/**diff --git a/lib/streaming/__tests__/create-chat-stream-response.test.ts b/lib/streaming/__tests__/create-chat-stream-response.test.tsindex b61bf5eb..75c90491 100644--- a/lib/streaming/__tests__/create-chat-stream-response.test.ts+++ b/lib/streaming/__tests__/create-chat-stream-response.test.ts@@ -34,7 +34,8 @@ vi.mock('@/instrumentation', () => ({}))vi.mock('@/lib/actions/chat', () => ({- loadChat: vi.fn()+ loadChat: vi.fn(),+ loadChatUncached: vi.fn()}))vi.mock('@/lib/agents/researcher', () => ({@@ -66,6 +67,7 @@ vi.mock('@/lib/utils/usage-logging', () => ({logUsage: vi.fn()}))+import { loadChat, loadChatUncached } from '@/lib/actions/chat'import { researcher } from '@/lib/agents/researcher'import { createChatStreamResponse } from '@/lib/streaming/create-chat-stream-response'import { describeStreamError } from '@/lib/streaming/helpers/describe-stream-error'@@ -161,6 +163,16 @@ describe('createChatStreamResponse', () => {)})+ it('loads an existing chat without the cache', async () => {+ mocks.stream.mockResolvedValue(createFakeResult())++ await createChatStreamResponse({ ...createConfig(), isNewChat: false })+ await mocks.finishPromise++ expect(loadChatUncached).toHaveBeenCalledWith('chat-id', 'user-id')+ expect(loadChat).not.toHaveBeenCalled()+ })+it('does not mark the span as failed for an aborted stream error', async () => {const streamError = new Error('request stopped')streamError.name = 'ResponseAborted'diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex c4f45ebd..8d3cd660 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -14,7 +14,7 @@ import {} from '@/lib/errors/tool-error'import { isTracingEnabled } from '@/lib/utils/telemetry'-import { loadChat } from '../actions/chat'+import { loadChatUncached } from '../actions/chat'import { generateChatTitle } from '../agents/title-generator'import {getMaxAllowedTokens,@@ -82,8 +82,9 @@ export async function createChatStreamResponse(let initialChat = nullif (!isNewChat) {const loadChatStart = performance.now()- // Fetch chat data for authorization check and cache it- initialChat = await loadChat(chatId, userId)+ // Read uncached: the prompt must not be built from a snapshot that+ // predates the previous turn's answer+ initialChat = await loadChatUncached(chatId, userId)perfTime('loadChat completed', loadChatStart)// Authorization check: if chat exists, it must belong to the user