2. Model and Engine - leeteresamaria/RoomieMatter GitHub Wiki
Story Map
Engine Architecture
Client
The client is able to sign in and out, create, join and leave rooms, see their roommates and their statuses, add, view, and edit both chores and events, chat with their roommates and an AI chatbot and change their own status by interacting both with Firebase functions and the Firebase database.
Signing In and Out
To sign in, users are first prompted when they open the app to sign in using their Google account on the AuthScreen. The sign in process is handled by Firebase.
Users are able to sign out on the ProfileView by clicking the "Log Out" button. The sign out process is handled by Firebase.
Creating, Joining and Leaving Rooms
To create a room, the user has to be signed in but not currently in a room. Once they click the create room button and name the room, createRoom is called which calls a Firebase function to create a new room.
To join a room, the user has to be signed in but not currently in a room. Once they click the join room button and enter the room token, joinRoom is called which calls a Firebase function to join the room.
To leave a room, the user has to be signed in and currently in a room. Once they click the leave room button on the ProfileView, quitRoom is called which calls a Firebase function to leave the room.
Seeing Roommate and Statuses
The user is able to see their roommates and corresponding statuses as well as themself on the HomeView. The roommates are fetched from the database. There is a listener on the status so each time it updates in the database, the new value is updated in the front end as well.
Add, View, and Edit Chores and Events
To add a chore/event, the user clicks the plus button next to All Chores/Events on the HomeView and fills out the corresponding details. saveChore or saveEvent are called based on if a chore or event is being added, with both of them calling a Firebase function.
To edit a chore/event, the user clicks the pencil icon in the top right of ExpandedChore/EventInfo and fills out the proper edits. This calls saveChore or saveEvent respectively, with both of them calling a Firebase function. If the user wants to delete a chore, there is a delete chore button that calls deleteChore which calls a Firebase function.
Chats
To chat with roommates and the ChatGPT housekeeper, the user clicks the message icon in the top right of LoggedInView to go to the ChatScreen. Here the user is able to send messages to there roommates or to the AI powered butler using @Housekeeper. Sending a chat calls sendChat which calls a Firebase function. The user is able to perform all the manual operations on events and chores in the chat.
There is a listener to the chats collection in the Firebase database, so every time the database has an update, getChats is called which calls a Firebase function.
Server
The server fetches, adds and edits chores and events in Google Calendar, communicates with ChatGPT and relays all the information to and from the client.
Fetch, Add, Delete and Edit Chores and Events
When the client requests chores/events, getChores/ getEvents are called which interact with the Google calendar to get the chores/events from the proper calendar and return them to the client.
When the client adds a chore/event, addChore/ addEvent are called which interact with the Google calendar to add the chore/event to the proper calendar.
When the client edits a chore/event, editChore/ editEvent are called which interact with the Google calendar to edit the chore/event in the proper calendar.
When the client deletes a chore/event, deleteChore/ deleteEvent are called which interact with the Google calendar to delete the chore/event in the proper calendar.
Rooms Backend
When a user creates a new room, createRoom is called. It creates a new room in the database, and creates a new events calendar and a new chores calendar in Google Calendar for the room.
When a user joins a room, quitRoom is called. It adds a document in the database that links the user to the room.
When a user leaves a room, quitRoom is called. It deletes the document in the database that link the user to the room.
Chats
When the user sends a chat, sendChat is called. It first checks to see if the chat is for the AI butler or just the room. Either way, the chat is posted in the chats collection in Firebase. If the chat is for the butler, it is sent to ChatGPT, along with a chat history and the available functions that can be called. ChatGPT determines the proper response and returns which function should be called along with necessary parameters. SendChat then parses the response from ChatGPT and executes the proper function. It sends ChatGPT the response from the function and ChatGPT interprets the response and returns a natural language version of the response. This natural language version is then added to the chats collection in the database.
Status
When the user changes their status, changeStatus is called. It changes the status in the database and returns if the operation was successful to the client.
Database
The database stores users, rooms, and chats. It is accessed by the client and the Firebase Functions.
Users Collection
The users collection stores most of each user's information. It has the displayName, image url, and id for each user.
User_rooms Collection
The user_rooms collection maps each user to their room, if a user is in a room. It also has the status for each user. It is accessed by the client through listening for the status and is accessed by the Firebase Functions through joining and leaving rooms as well as changing status.
Rooms Collection
The rooms collection holds the information for each room, such as name and calendarID. It is accessed by the database when rooms are created and deleted.
Chats Collection
The chats collection holds all the chats. It is accessed by the Firebase Functions through posting and fetching as well as the client through listening for changes. It holds who sent the chat, the chats contents as well as how many tokens have been used interacting with ChatGPT so far.
ChatGPT
ChatGPT forms a large part of our API usage, forming a core part of our functionality through the AI Butler Chat feature. Interaction with ChatGPT is accomplished when a user enters the chatbot view..
Functionality with the ChatGPT API consists of two general parts, the first is through text interacting with the AI Butler and asking questions about the saved information about the room, and the second is using the AI Butler to perform CRUD operations on shared events and chores. We will first discuss general chatting and how that is accomplished.
Chatting with ChatGPT
Interaction with ChatGPT is possible only when tagging the chatbot using @housekeeper, otherwise we do not interact with the API The client will fetch and display previous chats with pagination allowing users to jump to the most recent messages while also allowing browsing of past messages, including chats made by other users. The conversation is completed by sending prompts generated by the user when tagging the butler in their chat and recieving responses from the ChatGPT Server, completed using the OpenAI Javascript API. The server waits for a response to be received before adding it to the conversation. Conversation with the ChatGPT server is kept stateful by including a stored history of conversations, providing context to the LLM. This is accomplished here.
Managing Chores, Events, and status with ChatGPT
Outside of this general foundation for sending and receiving responses, the ChatGPT-powered AI butler can also use manage events and chores generated by participants of the app. This functionality is split across the chat.js file, as our general business involves determining what action the user is intending to enact, be it regarding chores, events or status. If the action involves Events or Status, the chat history is updated and then a handoff is done to the Google Calendar-related code.
Changes to status are made internally using the firebase to store state.
Google Calendar
Interactions with Google Calendar are completed through the calendar view and the chatbot view. through the aforementioned AI chatbot functionalities. Because we are interfacing directly with the Google Calendar API, we do not need to sync events nor store them on a database.
Via Chat View
As mentioned before, interactions with Google Calendar from the Chat View are accomplished using ChatGPT and are described as such:
Via Calendar View
Calendars are created by a participant and participants are added/invited to receive invites to either chores or events. The Calendar view is populated with events or chores depending on the date selected through the picker. These are obtained using the Google Calendar API for Javascript. Interactions with the Google Calendar server are split between Chores and Events.
From the UI, the user can enact different CRUD operations with the chores added to the calendar they are participating in such as: fetch chore, Update/edit chore, and Delete chore. The same functionality exists for Events: add event, Update/edit event. All of these functions simply call the following backend functions, which are what actually interact with the Google Calendar server that stores all this information retrieved and displayed on the views.
Backend:
The File calendar.js stores all of our CRUD operations with google calendar and can be subdivided into corresponding operations for Event and Chore: Get chores, Add chore, Edit chore, Complete chore, Delete chore, and Event Through these functionalities the users are able to directly interact with their shared calendar objects without leaving the app.