Building the Session Manager

Welcome back! In the previous lesson, we explored the importance of a robust system prompt and how it guides the behavior of our personal tutor. Now, we will delve into the next step of our journey: building the Session Manager. This lesson will focus on the model layer of the MVC (Model-View-Controller) architecture, which is crucial for organizing and managing data in a structured way. The SessionManager class will be the core component of our model layer, responsible for managing tutoring session data effectively using Laravel's session storage. By the end of this lesson, you will understand how to create, manage, and retrieve session data using the SessionManager class with Laravel's built-in session functionality.

Initializing the SessionManager

The SessionManager class is designed to handle the storage and management of tutoring session data using Laravel's session system. It serves as the backbone of our tutor's data management system. We'll start by setting up the class and then gradually add methods to handle session creation, message addition, and conversation retrieval.

Let's begin by defining the SessionManager class. Unlike traditional approaches that use instance variables, this class leverages Laravel's session storage to persist data across requests.

In this setup, we use Laravel's session() helper to store data in a nested array structure where the first key is the user_id, and the second key is the session_id. This structure allows us to efficiently manage multiple tutoring sessions for different users while persisting data across HTTP requests.

Creating a New Session

Next, we'll add the createSession method. This method is responsible for creating a new session entry for a user. It takes three parameters: $userId, $sessionId, and $systemPrompt.

The createSession method retrieves the current sessions from Laravel's session storage, checks if the $userId exists, and creates a new entry if needed. Then, it initializes the session with the provided $systemPrompt and an empty array for messages, finally storing the updated sessions back to Laravel's session.

Retrieving a Session

To access a specific tutoring session, we need the getSession method. This method retrieves a session based on the $userId and $sessionId.

The getSession method retrieves the sessions from Laravel's session storage and uses the null coalescing operator to safely access the nested array, returning the session data if it exists.

Adding Messages to a Session

Now, let's add the addMessage method. This method allows us to append messages to a session. It requires the $userId, $sessionId, $role, and $content of the message.

The addMessage method retrieves the current sessions, checks if the session exists, and if it does, appends the message to the session's message array before storing the updated sessions back to Laravel's session storage.

Retrieving the Full Conversation

Finally, we'll implement the getConversation method. This method returns the entire conversation, including the system prompt and all messages.

The getConversation method retrieves the session and constructs an array starting with the system prompt, followed by the session messages. If the session does not exist, it returns an empty array.

By following these steps, we've built a fully functional SessionManager class that can create and manage tutoring sessions, add messages, and retrieve conversation histories using Laravel's session system.

Creating and Managing Sessions

To create a new tutoring session, we use the createSession method. This method takes three parameters: $userId, $sessionId, and $systemPrompt. It initializes a new session entry in Laravel's session storage. If the $userId does not already exist in the sessions array, a new entry is created. The session entry includes the system prompt and an empty array for messages.

Here's an example of how to create a new tutoring session, including loading the system prompt from a file with proper error handling:

In this example, we define a loadSystemPrompt function that checks if the system prompt file exists and can be read. If the file is missing or cannot be read, a default prompt is used and an error message is displayed. We then initialize a SessionManager instance and create a new tutoring session for a user with the ID student_test. The session is identified by session_test and is initialized with a system prompt, all stored in Laravel's session.

Adding and Retrieving Messages

Once a session is created, we can add messages to it using the addMessage method. This method requires the $userId, $sessionId, $role, and $content of the message. The role indicates whether the message is from the student or the tutor assistant. The message is then appended to the session's message array and persisted in Laravel's session storage.

Retrieving Conversation

To retrieve the entire conversation, including the system prompt, we use the getConversation method. This method returns an array of messages, starting with the system prompt followed by the session messages.

Summary and Preparation for Practice

In this lesson, we explored the SessionManager class and its role in managing tutoring session data within the model layer of our Laravel application. We learned how to create and manage sessions, add messages, and retrieve conversation histories using Laravel's built-in session functionality. The SessionManager is a crucial component for organizing session data, ensuring that our personal tutor can handle multiple tutoring sessions efficiently while persisting data across HTTP requests.

As you move on to the practice exercises, take the opportunity to experiment with modifying and extending the SessionManager functionality. This hands-on practice will reinforce the concepts covered in this lesson and prepare you for the next steps in our course.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal