Comments (6)
@Godrules500 could you please raise a PR with the fix?
Fixes such as this one are super helpful for keeping the template updated.
from ai-chatbot.
having the same issue atm, were you able to fix it?
from ai-chatbot.
Hey @amxv yes, I was able to resolve it. Since I'm using gemini, I had to follow the AI SDK 3 example "next-ai-rsc". But essentially it should be the same thing.
Changes I made:
- Added a "saved" property to AIState (to prevent saving multiple times).
- Refactored saveChat called inside of unstable_onSetAIState to a method
- Called await saveChat when text completion "isFinal" was true.
I now call saveChat inside of unstable_onSetAIState like this
unstable_onSetAIState: async ({ state, done }) => {
'use server'
const session = await auth()
if (session && session.user) {
// only save it once it's done. No need to save it after each streamed result
if (done && !state.saved) {
await addOrUpdateChat(state, session)
state.saved = true
}
} else {
return
}
}
})
I then call await saveChat(state) when isFinal is true and set saved to true.
let state = aiState.get()
state.messages = [
...state.messages,
{
id: nanoid(),
role: 'assistant',
content
}
]
await saveChat(state)
reply.done()
aiState.done({ ...state, done: isFinal, saved: true })
And then the saveChat() is this
async function saveChat(state: AIState, session: Session | null = null) {
if (!session) {
session = await auth()
if (!session?.user?.id) {
throw new Error('User not Authorized to save')
}
}
const { chatId, messages } = state
const createdAt = Date.now()
const userId = session.user?.id as string
const path = `/chat/${chatId}`
const title = messages[0].content.substring(0, 100)
const chat: Chat = {
id: chatId,
title,
userId,
createdAt,
path,
messages
}
await dynamoDbService.saveChat(chat)
}
Let me know if that fixes it for you too or if you run into any issues with it please!
from ai-chatbot.
Experiencing the same issue.
from ai-chatbot.
Related Issues (20)
- Since ai/rsc can't work with google Vertex AI
- Signup Error HOT 2
- Sidebar in Shared Chat HOT 4
- iOS Safari Zoom Issue After Login
- useCopyToClipboard is not working in chat share dialog in Safari
- response streaming in loop and getting repeated
- I need help that
- ai/rsc with langchain HOT 3
- How to stop stream? HOT 2
- Connection closed error on Vercel HOT 15
- Role of Function call? HOT 1
- Q: Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server". HOT 4
- enviroment setup
- useEffect with no dependency array HOT 1
- Attempting to log out right after logging in does not work HOT 2
- Bot
- How to make the assistant write the first message? HOT 1
- Generative UI vs useChat? HOT 2
- AI response for the first user message after logging in doesn't show, until refresh HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ai-chatbot.