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
|
return
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onSetAIState: async ({ state }) => {
|
onSetAIState: async ({ state, done }) => {
|
||||||
'use server'
|
'use server'
|
||||||
|
|
||||||
|
if (!done) return
|
||||||
|
|
||||||
const session = await auth()
|
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 createdAt = new Date()
|
||||||
const userId = session.user.id as string
|
const userId = session.user.id as string
|
||||||
const path = `/chat/${chatId}`
|
const path = `/chat/${chatId}`
|
||||||
|
|
||||||
const firstMessageContent = messages[0].content as string
|
const firstMessageContent = messages[0].content as string
|
||||||
const title = firstMessageContent.substring(0, 100)
|
const title = firstMessageContent.substring(0, 100)
|
||||||
|
|
||||||
const chat: Chat = {
|
const chat: Chat = {
|
||||||
id: chatId,
|
id: chatId,
|
||||||
title,
|
title,
|
||||||
userId,
|
userId,
|
||||||
createdAt,
|
createdAt,
|
||||||
messages,
|
messages,
|
||||||
path
|
path
|
||||||
}
|
|
||||||
|
|
||||||
await saveChat(chat)
|
|
||||||
} else {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await saveChat(chat)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue