Example: Using Firebase
A guide to persisting chat history and threads for Thesys C1 GenUI SDK using Firebase Firestore as a persistence layer.
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
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:
-
Update Firebase Config File (
src/firebaseConfig.ts
): Replace the placeholder values infirebaseConfig
with your actual credentials insrc/firebaseConfig.ts
: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.
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.src/app/page.tsx (Hook Configuration)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
.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.src/app/api/chat/route.ts (Save Messages)Refer to the complete example
/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.
Test it Out!
Conclusion
By following this guide, you’ve integrated Firebase Firestore for robust chat persistence in your Thesys C1 GenUI application. This setup provides a scalable backend for your chat data, enhancing user experience by preserving conversation history.