Overview
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.Implementation Steps
1
Set up Firebase Project & SDK
First, ensure your Firebase project is ready and the Firebase SDK is configured in your Next.js application.
- Go to the Firebase Console,and copy the
firebaseConfig
object.
Here are the detailed step-by-step instructions on how to setup the database and fetch the firebase configuration:
Detailed Instructions
Detailed Instructions
-
Create Firestore Database:
- Create a new project or use an existing one in the Firebase Console.
- Navigate to “Firestore Database” and click “Create database”.
- Choose to start in Test mode for easier setup for the demo. This allows open read/write access for about 30 days.
- Important: Test mode rules are insecure and should not be used in a production environment.
-
Get Firebase Configuration:
- In your Firebase project, go to “Project settings” (gear icon) > “General” tab.
- Under “Your apps”, click “Add app” and select the Web platform (
</>
). - Register your app and Firebase will provide a
firebaseConfig
object. Copy this object.
-
Update Firebase Config File (
src/firebaseConfig.ts
): Replace the placeholder values infirebaseConfig
with your actual credentials insrc/firebaseConfig.ts
:src/firebaseConfig.ts
2
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
createThread
src/services/threadService.ts (createThread)
addMessages
addMessages
src/services/threadService.ts (addMessages)
3
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 theuseThreadListManager
anduseThreadManager
hooks.See the complete examplesrc/app/page.tsx (Hook Configuration)page.tsx
for detailed hook setup with imports.
4
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
.src/app/api/chat/route.ts (Fetch History) -
Save Messages on Stream End:
In the
on("end")
event of the LLM stream, useaddMessages
to store the new user prompt and all assistant/tool messages generated in that turn.Refer to the complete examplesrc/app/api/chat/route.ts (Save Messages)/api/chat/route.ts
for context.
Running and Testing
- Ensure your Firebase credentials are correctly set in
src/firebaseConfig.ts
. - Ensure your
THESYS_API_KEY
is set in.env
. - Run
npm run dev
. - Test creating new chats, sending messages, switching between chats, and refreshing the page to see if history persists. Check the Firebase console to see data being written to Firestore.
