Perisisting threads and chat history for your generative UI (GenUI) application using Firebase Firestore as a persistence layer.
This guide provides step-by-step instructions to implement chat persistence using Firebase Firestore within a Next.js application powered by the Thesys C1 GenUI SDK. By following this guide, you’ll learn how to store, retrieve, and manage chat threads and messages, enabling a seamless user experience across sessions.
The complete code for the example referenced in this guide can be found in the Thesys examples repository.
Set up Firebase Project & SDK
First, ensure your Firebase project is ready and the Firebase SDK is configured in your Next.js application.
firebaseConfig
object.Here are the detailed step-by-step instructions on how to setup the database and fetch the firebase configuration:
Detailed Instructions
Create Firestore Database:
Get Firebase Configuration:
</>
).firebaseConfig
object. Copy this object.Update Firebase Config File (src/firebaseConfig.ts
):
Replace the placeholder values in firebaseConfig
with your actual credentials in src/firebaseConfig.ts
:
Create Thread Service for Firebase
Implement a service layer that handles persistence logic using Firestore.
Create src/services/threadService.ts
:
1. Core CRUD Functions: Implement functions to create, read, update, and delete threads and messages.
Refer to the complete example threadService.ts
for full implementations of others like getThreadList
, getUIThreadMessages
, getLLMThreadMessages
, updateMessage
, deleteThread
, and updateThread
similarly.
createThread
addMessages
Integrate Service with Frontend
Configure C1Chat component (src/app/page.tsx
) to use the functions in threadService.ts
for data operations.
Configure C1 SDK Hooks:
Pass the threadService
functions to the useThreadListManager
and useThreadManager
hooks.
See the complete example page.tsx
for detailed hook setup with imports.
Add Backend Chat API Route
Add Chat Api route to use Thesys C1 Api along with functions in threadService.ts
for fetching historical messages and saving new ones.
Fetch History for LLM:
Before calling the LLM, fetch the existing messages for the thread using getLLMThreadMessages
.
Save Messages on Stream End:
In the on("end")
event of the LLM stream, use addMessages
to store the new user prompt and all assistant/tool messages generated in that turn.
Refer to the complete example /api/chat/route.ts
for context.
src/firebaseConfig.ts
.THESYS_API_KEY
is set in .env
.npm run dev
.Test it Out!
By following this guide, you’ve integrated Firebase Firestore for robust chat persistence in your conversational application. This setup provides a scalable backend for your chat data, enhancing user experience by preserving conversation history.
Perisisting threads and chat history for your generative UI (GenUI) application using Firebase Firestore as a persistence layer.
This guide provides step-by-step instructions to implement chat persistence using Firebase Firestore within a Next.js application powered by the Thesys C1 GenUI SDK. By following this guide, you’ll learn how to store, retrieve, and manage chat threads and messages, enabling a seamless user experience across sessions.
The complete code for the example referenced in this guide can be found in the Thesys examples repository.
Set up Firebase Project & SDK
First, ensure your Firebase project is ready and the Firebase SDK is configured in your Next.js application.
firebaseConfig
object.Here are the detailed step-by-step instructions on how to setup the database and fetch the firebase configuration:
Detailed Instructions
Create Firestore Database:
Get Firebase Configuration:
</>
).firebaseConfig
object. Copy this object.Update Firebase Config File (src/firebaseConfig.ts
):
Replace the placeholder values in firebaseConfig
with your actual credentials in src/firebaseConfig.ts
:
Create Thread Service for Firebase
Implement a service layer that handles persistence logic using Firestore.
Create src/services/threadService.ts
:
1. Core CRUD Functions: Implement functions to create, read, update, and delete threads and messages.
Refer to the complete example threadService.ts
for full implementations of others like getThreadList
, getUIThreadMessages
, getLLMThreadMessages
, updateMessage
, deleteThread
, and updateThread
similarly.
createThread
addMessages
Integrate Service with Frontend
Configure C1Chat component (src/app/page.tsx
) to use the functions in threadService.ts
for data operations.
Configure C1 SDK Hooks:
Pass the threadService
functions to the useThreadListManager
and useThreadManager
hooks.
See the complete example page.tsx
for detailed hook setup with imports.
Add Backend Chat API Route
Add Chat Api route to use Thesys C1 Api along with functions in threadService.ts
for fetching historical messages and saving new ones.
Fetch History for LLM:
Before calling the LLM, fetch the existing messages for the thread using getLLMThreadMessages
.
Save Messages on Stream End:
In the on("end")
event of the LLM stream, use addMessages
to store the new user prompt and all assistant/tool messages generated in that turn.
Refer to the complete example /api/chat/route.ts
for context.
src/firebaseConfig.ts
.THESYS_API_KEY
is set in .env
.npm run dev
.Test it Out!
By following this guide, you’ve integrated Firebase Firestore for robust chat persistence in your conversational application. This setup provides a scalable backend for your chat data, enhancing user experience by preserving conversation history.