Save to db on final ai state update (#429)

Co-authored-by: jb-dev1 <10735400+jb-dev1@users.noreply.github.com>
This commit is contained in:
Jeremy 2024-09-24 20:11:50 +05:30 committed by GitHub
parent 2a6bf4b00b
commit 124efca9a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -517,34 +517,33 @@ export const AI = createAI<AIState, UIState>({
return
}
},
onSetAIState: async ({ state }) => {
onSetAIState: async ({ state, done }) => {
'use server'
if (!done) return
const session = await auth()
if (!session || !session.user) return
if (session && session.user) {
const { chatId, messages } = state
const { chatId, messages } = state
const createdAt = new Date()
const userId = session.user.id as string
const path = `/chat/${chatId}`
const createdAt = new Date()
const userId = session.user.id as string
const path = `/chat/${chatId}`
const firstMessageContent = messages[0].content as string
const title = firstMessageContent.substring(0, 100)
const firstMessageContent = messages[0].content as string
const title = firstMessageContent.substring(0, 100)
const chat: Chat = {
id: chatId,
title,
userId,
createdAt,
messages,
path
}
await saveChat(chat)
} else {
return
const chat: Chat = {
id: chatId,
title,
userId,
createdAt,
messages,
path
}
await saveChat(chat)
}
})