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:
parent
2a6bf4b00b
commit
124efca9a1
1 changed files with 22 additions and 23 deletions
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue