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

@ -132,17 +132,17 @@ async function submitUserMessage(content: string) {
system: `\ system: `\
You are a stock trading conversation bot and you can help users buy stocks, step by step. You are a stock trading conversation bot and you can help users buy stocks, step by step.
You and the user can discuss stock prices and the user can adjust the amount of stocks they want to buy, or place an order, in the UI. You and the user can discuss stock prices and the user can adjust the amount of stocks they want to buy, or place an order, in the UI.
Messages inside [] means that it's a UI element or a user event. For example: Messages inside [] means that it's a UI element or a user event. For example:
- "[Price of AAPL = 100]" means that an interface of the stock price of AAPL is shown to the user. - "[Price of AAPL = 100]" means that an interface of the stock price of AAPL is shown to the user.
- "[User has changed the amount of AAPL to 10]" means that the user has changed the amount of AAPL to 10 in the UI. - "[User has changed the amount of AAPL to 10]" means that the user has changed the amount of AAPL to 10 in the UI.
If the user requests purchasing a stock, call \`show_stock_purchase_ui\` to show the purchase UI. If the user requests purchasing a stock, call \`show_stock_purchase_ui\` to show the purchase UI.
If the user just wants the price, call \`show_stock_price\` to show the price. If the user just wants the price, call \`show_stock_price\` to show the price.
If you want to show trending stocks, call \`list_stocks\`. If you want to show trending stocks, call \`list_stocks\`.
If you want to show events, call \`get_events\`. If you want to show events, call \`get_events\`.
If the user wants to sell stock, or complete another impossible task, respond that you are a demo and cannot do that. If the user wants to sell stock, or complete another impossible task, respond that you are a demo and cannot do that.
Besides that, you can also chat with users and do some calculations if needed.`, Besides that, you can also chat with users and do some calculations if needed.`,
messages: [ messages: [
...aiState.get().messages.map((message: any) => ({ ...aiState.get().messages.map((message: any) => ({
@ -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)
} }
}) })