Skip to content

An AI-powered search engine with a generative UI

fix(analytics): count the turn from user message ids instead of the chat (#1003)

Yoshiki Miuradbb30867299c
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index a52fb041..9b4d2cb9 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -1,13 +1,14 @@
import { revalidateTag } from 'next/cache'
import { cookies } from 'next/headers'
-import { loadChat } from '@/lib/actions/chat'
import {
calculateConversationTurn,
+ calculateConversationTurnFromIds,
deriveQueryShape,
trackChatEvent
} from '@/lib/analytics'
import { getCurrentUserId } from '@/lib/auth/get-current-user'
+import { getUserMessageIds } from '@/lib/db/actions'
import { generateId } from '@/lib/db/schema'
import { checkAndEnforceAdaptiveLimit } from '@/lib/rate-limit/adaptive-limit'
import { checkAndEnforceOverallChatLimit } from '@/lib/rate-limit/chat-limits'
@@ -189,7 +190,7 @@ export async function POST(req: Request) {
perfTime('createChatStreamResponse resolved', streamStart)
// Track analytics event (non-blocking)
- // Calculate conversation turn by loading chat history
+ // Calculate conversation turn
;(async () => {
try {
// Attribute to the authenticated user id, or the client-provided
@@ -201,13 +202,11 @@ export async function POST(req: Request) {
let conversationTurn = 1 // Default for new chats
if (!isNewChat) {
if (!isGuest && userId) {
- const chat = await loadChat(chatId, userId)
- if (chat?.messages) {
- conversationTurn = calculateConversationTurn(
- chat.messages,
- message?.id
- )
- }
+ const userMessageIds = await getUserMessageIds(chatId, userId)
+ conversationTurn = calculateConversationTurnFromIds(
+ userMessageIds,
+ message?.id
+ )
} else if (isGuest && Array.isArray(messages)) {
conversationTurn = calculateConversationTurn(messages, message?.id)
}
diff --git a/lib/analytics/__tests__/utils.test.ts b/lib/analytics/__tests__/utils.test.ts
index ce8c11d8..1c7399e2 100644
--- a/lib/analytics/__tests__/utils.test.ts
+++ b/lib/analytics/__tests__/utils.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest'
import {
calculateConversationTurn,
+ calculateConversationTurnFromIds,
deriveQueryShape
} from '@/lib/analytics/utils'
@@ -56,3 +57,25 @@ describe('calculateConversationTurn', () => {
expect(calculateConversationTurn(fresh)).toBe(2)
})
})
+
+describe('calculateConversationTurnFromIds', () => {
+ it('counts a current message when ids are empty', () => {
+ expect(calculateConversationTurnFromIds([], 'u1')).toBe(1)
+ })
+
+ it('counts ids without a current message id', () => {
+ expect(calculateConversationTurnFromIds(['u1', 'u2'])).toBe(2)
+ })
+
+ it('does not double count a current message already present', () => {
+ expect(calculateConversationTurnFromIds(['u1', 'u2'], 'u2')).toBe(2)
+ })
+
+ it('adds a current message not already present', () => {
+ expect(calculateConversationTurnFromIds(['u1', 'u2'], 'u3')).toBe(3)
+ })
+
+ it('returns at least 1', () => {
+ expect(calculateConversationTurnFromIds([])).toBe(1)
+ })
+})
diff --git a/lib/analytics/index.ts b/lib/analytics/index.ts
index 7acf6094..3f75df71 100644
--- a/lib/analytics/index.ts
+++ b/lib/analytics/index.ts
@@ -22,4 +22,8 @@ export type {
QueryLenBucket,
QueryShape
} from './types'
-export { calculateConversationTurn, deriveQueryShape } from './utils'
+export {
+ calculateConversationTurn,
+ calculateConversationTurnFromIds,
+ deriveQueryShape
+} from './utils'
diff --git a/lib/analytics/utils.ts b/lib/analytics/utils.ts
index cb397777..1ddad113 100644
--- a/lib/analytics/utils.ts
+++ b/lib/analytics/utils.ts
@@ -45,9 +45,17 @@ export function calculateConversationTurn(
messages: UIMessage[],
currentMessageId?: string
): number {
- const userIds = new Set(
- messages.filter(msg => msg.role === 'user').map(msg => msg.id)
+ return calculateConversationTurnFromIds(
+ messages.filter(msg => msg.role === 'user').map(msg => msg.id),
+ currentMessageId
)
+}
+
+export function calculateConversationTurnFromIds(
+ userMessageIds: string[],
+ currentMessageId?: string
+): number {
+ const userIds = new Set(userMessageIds)
if (currentMessageId) userIds.add(currentMessageId)
return Math.max(1, userIds.size)
}
diff --git a/lib/db/__tests__/user-message-ids.test.ts b/lib/db/__tests__/user-message-ids.test.ts
new file mode 100644
index 00000000..22979f8f
--- /dev/null
+++ b/lib/db/__tests__/user-message-ids.test.ts
@@ -0,0 +1,121 @@
+import { and, eq } from 'drizzle-orm'
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { messages } from '@/lib/db/schema'
+import { withOptionalRLS } from '@/lib/db/with-rls'
+
+import { getUserMessageIds } from '../actions'
+
+vi.mock('@/lib/db/with-rls', () => ({
+ withOptionalRLS: vi.fn(),
+ withRLS: vi.fn()
+}))
+
+function mockQueries({
+ chat,
+ messageRows = []
+}: {
+ chat?: { userId: string; visibility: 'private' | 'public' }
+ messageRows?: { id: string }[]
+}) {
+ const chatLimit = vi.fn().mockResolvedValue(chat ? [chat] : [])
+ const chatsWhere = vi.fn().mockReturnValue({ limit: chatLimit })
+ const messagesWhere = vi.fn().mockResolvedValue(messageRows)
+ const from = vi
+ .fn()
+ .mockReturnValueOnce({ where: chatsWhere })
+ .mockReturnValueOnce({ where: messagesWhere })
+ const select = vi.fn().mockReturnValue({ from })
+
+ vi.mocked(withOptionalRLS).mockImplementation(async (_userId, callback) =>
+ callback({ select } as never)
+ )
+
+ return { select, messagesWhere }
+}
+
+describe('getUserMessageIds', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ })
+
+ it('returns an empty list without querying messages when the chat is not found', async () => {
+ const { select, messagesWhere } = mockQueries({})
+
+ const result = await getUserMessageIds('chat-1', 'user-1')
+
+ expect(result).toEqual([])
+ expect(select).toHaveBeenCalledTimes(1)
+ expect(messagesWhere).not.toHaveBeenCalled()
+ expect(withOptionalRLS).toHaveBeenCalledWith('user-1', expect.any(Function))
+ })
+
+ it("returns an empty list without querying messages for another user's private chat", async () => {
+ const { select, messagesWhere } = mockQueries({
+ chat: { userId: 'user-2', visibility: 'private' }
+ })
+
+ const result = await getUserMessageIds('chat-1', 'user-1')
+
+ expect(result).toEqual([])
+ expect(select).toHaveBeenCalledTimes(1)
+ expect(messagesWhere).not.toHaveBeenCalled()
+ expect(withOptionalRLS).toHaveBeenCalledWith('user-1', expect.any(Function))
+ })
+
+ it('returns an empty list without querying messages for a private chat without a user ID', async () => {
+ const { select, messagesWhere } = mockQueries({
+ chat: { userId: 'user-1', visibility: 'private' }
+ })
+
+ const result = await getUserMessageIds('chat-1')
+
+ expect(result).toEqual([])
+ expect(select).toHaveBeenCalledTimes(1)
+ expect(messagesWhere).not.toHaveBeenCalled()
+ expect(withOptionalRLS).toHaveBeenCalledWith(null, expect.any(Function))
+ })
+
+ it('returns private chat message ids in order for the owner', async () => {
+ const { select, messagesWhere } = mockQueries({
+ chat: { userId: 'user-1', visibility: 'private' },
+ messageRows: [{ id: 'message-2' }, { id: 'message-1' }]
+ })
+
+ const result = await getUserMessageIds('chat-1', 'user-1')
+
+ expect(result).toEqual(['message-2', 'message-1'])
+ expect(select).toHaveBeenCalledTimes(2)
+ expect(messagesWhere).toHaveBeenCalledExactlyOnceWith(
+ and(eq(messages.chatId, 'chat-1'), eq(messages.role, 'user'))
+ )
+ expect(withOptionalRLS).toHaveBeenCalledWith('user-1', expect.any(Function))
+ })
+
+ it('returns public chat message ids for a different user', async () => {
+ const { select, messagesWhere } = mockQueries({
+ chat: { userId: 'user-2', visibility: 'public' },
+ messageRows: [{ id: 'message-1' }, { id: 'message-2' }]
+ })
+
+ const result = await getUserMessageIds('chat-1', 'user-1')
+
+ expect(result).toEqual(['message-1', 'message-2'])
+ expect(select).toHaveBeenCalledTimes(2)
+ expect(messagesWhere).toHaveBeenCalledTimes(1)
+ expect(withOptionalRLS).toHaveBeenCalledWith('user-1', expect.any(Function))
+ })
+
+ it('maps message rows to a plain string array', async () => {
+ mockQueries({
+ chat: { userId: 'user-1', visibility: 'private' },
+ messageRows: [{ id: 'message-1' }, { id: 'message-2' }]
+ })
+
+ const result = await getUserMessageIds('chat-1', 'user-1')
+
+ expect(result).toEqual(['message-1', 'message-2'])
+ expect(result.every(id => typeof id === 'string')).toBe(true)
+ expect(withOptionalRLS).toHaveBeenCalledWith('user-1', expect.any(Function))
+ })
+})
diff --git a/lib/db/actions.ts b/lib/db/actions.ts
index 56ef91db..4f69e83a 100644
--- a/lib/db/actions.ts
+++ b/lib/db/actions.ts
@@ -218,6 +218,37 @@ export async function loadChatWithMessages(
})
}
+export async function getUserMessageIds(
+ chatId: string,
+ userId?: string
+): Promise<string[]> {
+ const count = incrementDbOperationCount()
+ perfLog(`DB - getUserMessageIds called - count: ${count}`)
+
+ return withOptionalRLS(userId || null, async tx => {
+ const [chat] = await tx
+ .select()
+ .from(chats)
+ .where(eq(chats.id, chatId))
+ .limit(1)
+
+ if (!chat) {
+ return []
+ }
+
+ if (chat.visibility === 'private' && (!userId || chat.userId !== userId)) {
+ return []
+ }
+
+ const userMessages = await tx
+ .select({ id: messages.id })
+ .from(messages)
+ .where(and(eq(messages.chatId, chatId), eq(messages.role, 'user')))
+
+ return userMessages.map(message => message.id)
+ })
+}
+
/**
* Delete messages after a specific message
*/